当前位置:首页 > 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);

添加干扰元素

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

// 添加干扰线
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;

验证用户输入

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

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实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

php实现增删改查

php实现增删改查

以下是在PHP中实现增删改查(CRUD)操作的详细方法,以MySQL数据库为例: 数据库连接配置 创建config.php文件存储数据库连接信息: <?php $host = 'localh…

php 实现购物车

php 实现购物车

PHP 实现购物车功能 购物车功能是电子商务网站的核心部分,可以通过 PHP 和 Session 或数据库来实现。以下是两种常见的实现方式: 使用 Session 存储购物车数据 Sessi…

php实现异步

php实现异步

PHP 实现异步的方法 PHP 本身是同步执行的脚本语言,但可以通过以下方法模拟异步操作或实现异步效果: 使用多进程(pcntl_fork) 通过 pcntl_fork 创建子进程实现异步,适用于…

uniapp图形验证码

uniapp图形验证码

uniapp 图形验证码实现方法 在 uniapp 中实现图形验证码功能可以通过多种方式完成,以下是几种常见的实现方案: 使用第三方验证码服务 接入第三方验证码服务如极验、腾讯云验证码等,这些服务通…

php实现统计

php实现统计

PHP 实现统计功能 在 PHP 中实现统计功能通常涉及数据的收集、处理和展示。以下是一些常见的统计实现方法: 数据库数据统计 通过 SQL 查询可以直接从数据库中获取统计结果: $conn =…