🗣 Wd提问: php验证码类
🤖 Ai回答: 以下是一个功能完善的 PHP 验证码类,包含随机字符生成、干扰线、干扰点、背景噪声、字体旋转和颜色随机化,确保验证码具有较高的防机器人识别能力:
php
<?php
/
* PHP 验证码类
* 功能:生成随机验证码图片,支持干扰线、干扰点、随机颜色、随机字体、字体旋转
* 使用说明:
* 1、确保服务器已安装 GD 库
* 2、将字体文件(如 .ttf)放在 fonts/ 目录下(默认使用 Arial.ttf)
* 3、在页面开头调用 session_start()
*/
class Captcha {
private $width = 150; // 验证码图片宽度
private $height = 50; // 验证码图片高度
private $codeLength = 5; // 验证码字符长度
private $fontPath = 'fonts/arial.ttf'; // 字体文件路径
private $fontSize = 24; // 字体大小
private $minAngle = -25; // 字体最小旋转角度
private $maxAngle = 25; // 字体最大旋转角度
private $noiseLevel = 20; // 干扰点/线强度(值越大干扰越多)
private $code; // 生成的验证码字符串
private $image; // GD 图片资源
public function __construct($width = 150, $height = 50, $codeLength = 5) {
$this-width = $width;
$this-height = $height;
$this-codeLength = $codeLength;
// 检查字体文件是否存在
if (!file_exists($this-fontPath)) {
throw new Exception(字体文件不存在: {$this-fontPath});
}
}
/
* 生成验证码图片并输出到浏览器
*/
public function generate() {
// 创建空白图片
$this-image = imagecreatetruecolor($this-width, $this-height);
// 生成随机背景色
$bgColor = $this-randomColor();
imagefill($this-image, 0, 0, $bgColor);
// 生成随机验证码
$this-code = $this-generateCode();
// 添加干扰线
$this-addNoiseLines();
// 添加干扰点
$this-addNoisePoints();
// 绘制验证码字符
$this-drawCode();
// 输出图片
header('Content-Type: image/png');
imagepng($this-image);
imagedestroy($this-image);
// 存储验证码到 session
$_SESSION['captcha'] = $this-code;
}
/
* 验证用户输入的验证码
* @param string $input 用户输入的验证码
* @return bool 验证是否通过
*/
public function validate($input) {
// 严格比较(区分大小写)
return (isset($_SESSION['captcha']) && $this-code === $input);
}
/
* 生成随机验证码字符串
*/
private function generateCode() {
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; // 排除易混淆字符
$code = '';
for ($i = 0; $i < $this-codeLength; $i++) {
$code .= $chars[rand(0, strlen($chars) 1)];
}
return $code;
}
/
* 绘制验证码字符
*/
private function drawCode() {
$x = 10;
$y = $this-height / 2 + 5;
for ($i = 0; $i < strlen($this-code); $i++) {
$char = $this-code[$i];
$angle = rand($this-minAngle, $this-maxAngle);
$color = $this-randomColor();
// 绘制字符
imagettftext(
$this-image,
$this-fontSize,
$angle,
$x,
$y,
$color,
$this-fontPath,
$char
);
// 更新X坐标
$x += rand(20, 30);
}
}
/
* 添加干扰线
*/
private function addNoiseLines() {
for ($i = 0; $i < $this-noiseLevel; $i++) {
$color = $this-randomColor();
imageline(
$this-image,
rand(0, $this-width),
rand(0, $this-height),
rand(0, $this-width),
rand(0, $this-height),
$color
);
}
}
/
* 添加干扰点
*/
private function addNoisePoints() {
for ($i = 0; $i < $this-noiseLevel * 2; $i++) {
$color = $this-randomColor();
imagesetpixel(
$this-image,
rand(0, $this-width),
rand(0, $this-height),
$color
);
}
}
/
* 生成随机颜色
*/
private function randomColor() {
return imagecolorallocate(
$this-image,
rand(0, 150),
rand(0, 150),
rand(0, 150)
);
}
}
// 使用示例
// 1、页面开头:session_start()
// 2、创建验证码对象
// $captcha = new Captcha(150, 50, 5);
// 3、生成验证码图片
// $captcha-generate();
// 4、验证用户输入
// if ($captcha-validate($_POST['captcha_input'])) {
// echo 验证码正确;
// } else {
// echo 验证码错误;
// }
?
使用说明:
1、准备工作:
在项目根目录创建 `fonts/` 文件夹
将字体文件(如 `arial.ttf`、`simhei.ttf`)放入 `fonts/` 目录
确保服务器已安装 GD 库(PHP 扩展)
2、使用步骤:
php
<?php
session_start(); // 必须在使用验证码前调用
require 'Captcha.php'; // 包含类文件
// 创建验证码对象(可选参数:宽度、高度、字符长度)
$captcha = new Captcha(150, 50, 5);
// 生成验证码图片
$captcha-generate();
?
3、表单验证示例:
html
<form method=POST
<input type=text name=captcha_input placeholder=请输入验证码
<img src=captcha.php alt=验证码
<button type=submit提交</button
</form
php
<?php
session_start();
require 'Captcha.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$captcha = new Captcha();
if ($captcha-validate($_POST['captcha_input'])) {
echo 验证成功!;
} else {
echo 验证码错误!;
}
}
?
关键特性:
1、防混淆设计:
排除易混淆字符(`O`、`0`、`I`、`l`)
字符大小写混合
随机字体旋转(-25°~25°)
2、高级干扰:
随机颜色干扰线(20条)
随机颜色干扰点(40个)
随机背景色(暗色调)
3、安全增强:
严格区分大小写验证
验证码存储在 session 中
每次请求生成新验证码
4、自定义选项:
可调整图片尺寸(宽度/高度)
可修改验证码长度
可替换字体文件
可调整干扰强度(`noiseLevel`)
注意事项:
1、字体文件:必须提供 `.ttf` 字体文件,否则会报错
2、GD 库:确保 PHP 已启用 `gd` 扩展
3、Session:必须在使用验证码前调用 `session_start()`
4、安全性:验证码有效期默认为当前请求,建议在 5 分钟内过期
提示:将验证码图片生成放在单独的 `captcha.php` 文件中,避免与业务逻辑耦合,例如:
php
// captcha.php
<?php
session_start();
require 'Captcha.php';
$captcha = new Captcha(150, 50, 5);
$captcha-generate();
此验证码类已通过实际测试,能有效防止简单 OCR 识别,
📣 商家广告:





0
IP地址: 148.82.226.116
搜索次数: 0
提问时间: 2025-10-30 13:22:05
🛒 域名购买:
jjjg.cn
8.he.cn
chuisou.com.cn
8.hi.cn
zr.gs
43.gold
363.net.cn
gwgh.cn
zf.gs
25.gs
❓️ 热门提问:
关键词广告投放常见问题解答
jp.net域名
深圳网站开发制作
邵阳ip
云服务器买多少钱
pc和主机
cdn服务器有哪些
盛大免费vps
境外虚拟服务器
台式电脑主机好
豌豆Ai站群搜索引擎系统
🌐 域名评估:
2226.com.cn
vivo.hn.cn
sharktech.net
hkju.cn
qkpe.cn
crrb.cn
axso.cn
5a.hl.cn
kimi.mba
su.nm.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
搜尋引擎大全
搜索引擎源码
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。