当前位置:首页 > PHP

php验证码怎么实现

2026-02-27 20:44:52PHP

生成随机验证码字符串

在PHP中生成验证码通常需要创建一个随机字符串,包含数字、字母或其组合。以下是一个生成4位随机数字验证码的示例:

$length = 4;
$code = substr(str_shuffle("0123456789"), 0, $length);

如果需要包含字母,可以修改为:

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code = substr(str_shuffle($chars), 0, $length);

创建验证码图片

使用GD库将验证码字符串渲染为图片:

header('Content-Type: image/png');
$image = imagecreatetruecolor(200, 50);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, 200, 50, $bgColor);

$textColor = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, 20, 0, 50, 35, $textColor, 'path/to/font.ttf', $code);

imagepng($image);
imagedestroy($image);

添加干扰元素

为提高安全性,可以添加干扰线和噪点:

php验证码怎么实现

// 添加干扰线
for ($i = 0; $i < 5; $i++) {
    $lineColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, 200), rand(0, 50), rand(0, 200), rand(0, 50), $lineColor);
}

// 添加噪点
for ($i = 0; $i < 100; $i++) {
    $pixelColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, 200), rand(0, 50), $pixelColor);
}

存储验证码到Session

将生成的验证码存入Session以便后续验证:

session_start();
$_SESSION['captcha'] = $code;

验证用户输入

在表单提交处理页面验证用户输入:

php验证码怎么实现

session_start();
if (strtolower($_POST['captcha']) !== strtolower($_SESSION['captcha'])) {
    die('验证码错误');
}
unset($_SESSION['captcha']);

完整示例代码

将以上步骤整合为一个完整的验证码生成和验证流程:

// captcha.php
session_start();
$length = 6;
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code = substr(str_shuffle($chars), 0, $length);
$_SESSION['captcha'] = $code;

header('Content-Type: image/png');
$image = imagecreatetruecolor(200, 50);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, 200, 50, $bgColor);

$textColor = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, 20, 0, 50, 35, $textColor, 'arial.ttf', $code);

// 干扰线和噪点
for ($i = 0; $i < 5; $i++) {
    imageline($image, rand(0, 200), rand(0, 50), rand(0, 200), rand(0, 50), 
        imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)));
}
for ($i = 0; $i < 100; $i++) {
    imagesetpixel($image, rand(0, 200), rand(0, 50), 
        imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)));
}

imagepng($image);
imagedestroy($image);

在HTML表单中引用:

<img src="captcha.php" onclick="this.src='captcha.php?'+Math.random()">
<input type="text" name="captcha" required>

验证页面:

session_start();
if (empty($_SESSION['captcha']) || strtolower($_POST['captcha']) !== strtolower($_SESSION['captcha'])) {
    die('验证码错误');
}
unset($_SESSION['captcha']);
// 继续处理表单

标签: 验证码php
分享给朋友:

相关文章

php实现上传

php实现上传

PHP 文件上传实现 HTML 表单设置 创建一个包含文件上传字段的 HTML 表单,确保表单的 enctype 属性设置为 multipart/form-data,这是文件上传必需的设置。 <…

php实现文件的下载

php实现文件的下载

PHP 实现文件下载的方法 在 PHP 中实现文件下载功能通常需要设置正确的 HTTP 头信息,并输出文件内容。以下是几种常见的实现方式: 使用 header() 函数强制下载 通过设置 Conte…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

php实现下载

php实现下载

PHP实现文件下载 使用PHP实现文件下载可以通过设置HTTP头部信息强制浏览器下载文件而非直接打开。以下是几种常见的方法: 方法一:直接输出文件内容 $file_path = 'path/to/…

php 实现类

php 实现类

PHP 类的基本实现 在 PHP 中,类通过 class 关键字定义,后跟类名和一对花括号。类名通常遵循大驼峰命名法(如 ClassName)。以下是基本语法: class ClassName {…

php实现mvc

php实现mvc

PHP 实现 MVC 架构 MVC(Model-View-Controller)是一种常用的软件设计模式,将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller…