豌豆Ai站群搜索引擎系统 V.25.10.25 网址:www.wd.chat

🗣 Wd提问: PHP源码图片验证码

🤖 Ai回答:
在PHP中生成图片验证码(也称为CAPTCHA)是一种常见的安全措施,用于防止自动化脚本提交表单。下面是一个完整的、基于PHP GD库的简单图片验证码示例。

✅ 功能说明:
生成随机4位数字或字母组合
添加干扰线条和噪点
输出为PNG图像
将验证码值保存到Session中以便后续验证

📁 文件结构建议:

captcha.php // 生成验证码图片
form.html // 表单页面
verify.php // 验证用户输入

1、`captcha.php` 生成验证码图片

php
<?php
// 启动会话
session_start();

// 设置图片尺寸
$width = 120;
$height = 40;

// 创建画布
$image = imagecreate($width, $height);

// 定义颜色(背景色、文字色、干扰线/点)
$bgColor = imagecolorallocate($image, 240, 240, 240); // 浅灰背景
$textColor = imagecolorallocate($image, 0, 0, 0); // 黑色文字
$lineColor = imagecolorallocate($image, 150, 150, 150); // 灰色干扰线
$noiseColor = imagecolorallocate($image, 200, 200, 200); // 噪点颜色

// 生成随机验证码(4位数字+大写字母)
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$captcha = '';
for ($i = 0; $i < 4; $i++) {
$captcha .= $chars[rand(0, strlen($chars) 1)];
}

// 存储到 session
$_SESSION['captcha'] = $captcha;

// 在图像上绘制文本(使用内置字体)
$font = 5; // 内置字体大小(5是最大内置字体)
$x = ($width (imagefontwidth($font) * strlen($captcha))) / 2;
$y = ($height imagefontheight($font)) / 2;
imagestring($image, $font, $x, $y, $captcha, $textColor);

// 添加干扰线
for ($i = 0; $i < 3; $i++) {
imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
}

// 添加噪点
for ($i = 0; $i < 50; $i++) {
imagesetpixel($image, rand(0, $width), rand(0, $height), $noiseColor);
}

// 输出图片为PNG
header('Content-Type: image/png');
imagepng($image);

// 释放内存
imagedestroy($image);
?

2、`form.html` 显示验证码和输入框

html
<!DOCTYPE html
<html lang=zh
<head
<meta charset=UTF-8
<title验证码测试</title
</head
<body
<h2请输入验证码</h2
<form action=verify.php method=post
<p
验证码:<br
<img src=captcha.php alt=验证码 onclick=this.src='captcha.php?'+Math.random(); style=cursor:pointer;
<br
(点击图片刷新)
</p
<p
<input type=text name=captcha required
</p
<p
<button type=submit提交</button
</p
</form
</body
</html

🔁 点击刷新:通过 JS 更改图片 URL 加时间戳来刷新验证码。

3、`verify.php` 验证用户输入

php
<?php
session_start();

if ($_POST['captcha']) {
$userInput = strtoupper(trim($_POST['captcha'])); // 转大写统一处理
$correctCaptcha = $_SESSION['captcha'];

if ($userInput === $correctCaptcha) {
echo <h2 style='color:green;'✅ 验证成功!</h2;
} else {
echo <h2 style='color:red;'❌ 验证失败,请重试。</h2;
echo <a href='form.html'返回</a;
}
} else {
echo 非法访问;
}
?

✅ 注意事项:

1、GD扩展必须启用
确保你的PHP环境已开启GD库(用于图像处理):
bash
php -m | grep gd

若未安装,在`php.ini`中启用:
ini
extension=gd


2、安全性增强建议:
验证码区分大小写?通常转为大写比较。
设置过期时间(如5分钟后失效)。
使用更复杂的字体(TTF)提升可读性与防识别能力。
可加入扭曲、旋转等效果。

3、进阶推荐:
使用 TrueType 字体让验证码更美观:
php
imagettftext($image, 16, $angle, $x, $y, $textColor, 'arial.ttf', $captcha);


✅ 效果预览

打开 `form.html`,你会看到一个带验证码图片的表单,用户输入后提交到 `verify.php` 进行比对。

如有需要,我也可以提供:
中文验证码版本
数学算式验证码(如“3 + 5 = ?”)
Composer封装版本

是否需要更高级的样式或安全功能?欢迎继续提问!

Ai作答

📣 商家广告


域名评估

起名

广东过滤器

Ai评估

优质域名


0

IP地址: 8.184.138.136

搜索次数: 95

提问时间: 2025-11-21 13:30:11

🛒 域名购买

❓️ 热门提问

🌐 域名评估

最新挖掘

🖌 热门作画

🤝 关于我们

🗨 加入群聊

🔗 友情链接

🧰 站长工具
📢

温馨提示

本站所有 问答 均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。

👉

技术支持

本站由 🟢 豌豆Ai 提供技术支持,使用的最新版: 豌豆Ai站群搜索引擎系统 V.25.10.25 搭建本站。

上一篇 84096 84097 84098 下一篇