当前位置:首页 > PHP

php实现变色验证码

2026-01-29 06:38:20PHP

生成随机验证码

使用PHP的rand()mt_rand()函数生成随机字符串作为验证码。可以组合数字和字母增加复杂度。

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i < 4; $i++) {
    $code .= $chars[mt_rand(0, strlen($chars) - 1)];
}

创建画布并设置背景

使用imagecreatetruecolor()函数创建画布,设置背景色为白色或其他浅色。

$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

添加干扰元素

在画布上添加干扰线或干扰点,防止机器识别。使用随机颜色和位置绘制线条或点。

for ($i = 0; $i < 5; $i++) {
    $color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
}

for ($i = 0; $i < 50; $i++) {
    $color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $color);
}

绘制变色验证码

将验证码字符绘制到画布上,每个字符使用不同的颜色和位置。

for ($i = 0; $i < strlen($code); $i++) {
    $charColor = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
    $x = ($width / strlen($code)) * $i + mt_rand(5, 10);
    $y = mt_rand(5, $height - 15);
    imagestring($image, 5, $x, $y, $code[$i], $charColor);
}

输出图像并保存验证码

将图像输出为PNG或JPEG格式,同时将验证码存入Session以便后续验证。

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

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

完整代码示例

以下是一个完整的PHP变色验证码生成代码:

<?php
session_start();

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i < 4; $i++) {
    $code .= $chars[mt_rand(0, strlen($chars) - 1)];
}

$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

for ($i = 0; $i < 5; $i++) {
    $color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
}

for ($i = 0; $i < 50; $i++) {
    $color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $color);
}

for ($i = 0; $i < strlen($code); $i++) {
    $charColor = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
    $x = ($width / strlen($code)) * $i + mt_rand(5, 10);
    $y = mt_rand(5, $height - 15);
    imagestring($image, 5, $x, $y, $code[$i], $charColor);
}

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

$_SESSION['captcha'] = $code;
?>

验证码验证

在表单提交时,通过比较Session中的验证码和用户输入的值来验证。

php实现变色验证码

session_start();
if ($_POST['captcha'] === $_SESSION['captcha']) {
    echo '验证码正确';
} else {
    echo '验证码错误';
}

通过以上步骤,可以实现一个简单的变色验证码功能,有效防止自动化工具的攻击。

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

相关文章

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php 实现路由

php 实现路由

PHP 实现路由的方法 在 PHP 中实现路由功能可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 PHP 实现简单路由 通过解析 URL 并匹配对应的处理逻辑,可以实现基本的路由功能。以…

php实现单例模式

php实现单例模式

单例模式的概念 单例模式是一种创建型设计模式,确保一个类只有一个实例,并提供一个全局访问点。这种模式常用于数据库连接、日志记录器等需要全局唯一实例的场景。 PHP实现单例模式的关键步骤 私有化构造函…

php实现发帖

php实现发帖

PHP 实现发帖功能 要在 PHP 中实现发帖功能,通常需要结合数据库操作(如 MySQL)和前端表单提交。以下是实现发帖功能的关键步骤: 数据库表设计 创建一个帖子表(posts)存储发帖内容,基…

php实现des加密

php实现des加密

DES 加密实现方法 在 PHP 中实现 DES 加密可以通过内置的 mcrypt 或 openssl 扩展完成。由于 mcrypt 已在 PHP 7.2 中废弃,推荐使用 openssl 扩展。…

php评论功能实现

php评论功能实现

数据库设计 创建一个评论表存储评论数据,表结构可包含以下字段: id:自增主键 content:评论内容(TEXT类型) user_id:关联用户表的ID article_id:关联文章表的ID c…