当前位置:首页 > PHP

php实现验证码

2026-01-13 12:31:03PHP

PHP实现验证码的方法

使用GD库生成验证码

GD库是PHP中处理图像的扩展,可用于生成验证码图片。

<?php
session_start();
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

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

for ($i = 0; $i < 4; $i++) {
    $textColor = imagecolorallocate($image, rand(0, 150), rand(0, 150), rand(0, 150));
    imagettftext($image, 20, rand(-30, 30), 10 + $i * 30, 30, $textColor, 'arial.ttf', $code[$i]);
}

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

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

使用HTML表单显示验证码

在HTML页面中显示生成的验证码图片:

<img src="captcha.php" onclick="this.src='captcha.php?'+Math.random()" style="cursor:pointer" title="点击刷新验证码">

验证用户输入

验证用户提交的验证码是否正确:

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

增加干扰线和扭曲效果

为了增强安全性,可以添加干扰线和扭曲效果:

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

// 扭曲效果
$distortionImage = imagecreatetruecolor($width, $height);
imagefill($distortionImage, 0, 0, $bgColor);
for ($x = 0; $x < $width; $x++) {
    for ($y = 0; $y < $height; $y++) {
        $newX = $x + sin($y / 10) * 5;
        $newY = $y + sin($x / 10) * 5;
        $color = imagecolorat($image, $x, $y);
        imagesetpixel($distortionImage, $newX, $newY, $color);
    }
}

使用Composer包

可以使用现成的Composer包来简化验证码生成:

composer require gregwar/captcha
<?php
require 'vendor/autoload.php';
use Gregwar\Captcha\CaptchaBuilder;

$builder = new CaptchaBuilder;
$builder->build();
$_SESSION['phrase'] = $builder->getPhrase();
header('Content-type: image/jpeg');
$builder->output();
?>

注意事项

  • 确保服务器已安装GD库
  • 验证码应包含大小写字母和数字组合
  • 验证码应在会话中存储
  • 验证码图片应设置适当的过期头
  • 可以考虑限制验证码尝试次数防止暴力破解

php实现验证码

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

相关文章

vue实现验证码

vue实现验证码

Vue 实现验证码的方法 使用 Canvas 生成图形验证码 安装依赖(如需要): npm install vue-canvas-verify --save 创建验证码组件: <te…

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_sh…

php如何实现直播

php如何实现直播

实现直播功能的方法 PHP可以通过结合其他技术和工具来实现直播功能。以下是几种常见的方法: 使用流媒体服务器 配置流媒体服务器如Nginx-RTMP、Red5或Wowza。这些服务器支持RTM…

php实现文件的下载

php实现文件的下载

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

php 实现单链表

php 实现单链表

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

php实现https

php实现https

PHP 实现 HTTPS 请求 在 PHP 中实现 HTTPS 请求,可以通过多种方式完成,包括使用内置函数、cURL 扩展或第三方库。以下是几种常见的方法: 使用 file_get_content…