当前位置:首页 > 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页面中显示生成的验证码图片:

php实现验证码

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

验证用户输入

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

php实现验证码

<?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实现打印功能

php实现打印功能

PHP实现打印功能的方法 在PHP中实现打印功能可以通过多种方式完成,包括直接输出到浏览器、生成PDF文件、调用打印机接口等。以下是几种常见的方法: 直接输出HTML内容 PHP可以通过echo或…

php实现分页

php实现分页

PHP实现分页的基本方法 使用原生PHP实现分页功能需要结合数据库查询和HTML展示。以下是核心实现步骤: 数据库查询与分页逻辑 // 连接数据库 $conn = new mysqli('lo…

php实现文件上传

php实现文件上传

文件上传的基本实现 在PHP中实现文件上传需要使用$_FILES超全局数组处理上传的文件数据。表单必须设置enctype="multipart/form-data"属性,并采用POST方法提交。…

php实现搜索功能

php实现搜索功能

实现基础搜索功能 使用PHP和MySQL实现基础的搜索功能需要结合表单提交与数据库查询。创建一个HTML表单用于接收用户输入的搜索关键词,通过PHP处理表单数据并查询数据库。 // 搜索表单 (H…

vue实现验证码

vue实现验证码

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

php实现打印功能

php实现打印功能

PHP 实现打印功能的方法 PHP 可以通过多种方式实现打印功能,具体取决于打印的目标(如打印机、PDF、网页等)和需求(如直接打印、生成打印预览等)。以下是几种常见的实现方式: 使用 JavaS…