当前位置:首页 > 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 长连接实现方法 长连接(Keep-Alive)允许客户端与服务器在一次TCP连接中发送和接收多个HTTP请求/响应,减少重复建立连接的开销。以下是PHP中实现长连接的几种方式: 配置HTTP…

php验证码实现

php验证码实现

验证码实现方法 在PHP中实现验证码功能通常涉及生成随机字符串或数字,将其转换为图像,并通过会话(Session)进行验证。以下是几种常见的实现方式: 使用GD库生成图像验证码 GD库是PHP内置的…

php 实现验证码

php 实现验证码

使用 PHP 生成验证码 验证码(CAPTCHA)用于防止自动化脚本提交表单,以下是几种常见的 PHP 实现方法。 使用 GD 库生成图片验证码 GD 库是 PHP 的图像处理扩展,可用于生成验证码…

基于php的设计与实现

基于php的设计与实现

PHP 设计与实现的关键要素 环境搭建与框架选择 PHP开发需配置服务器环境(如Apache/Nginx)、PHP版本(建议7.4+)及数据库(MySQL/MariaDB)。主流框架包括Laravel…

php mvc实现

php mvc实现

PHP MVC 实现步骤 模型(Model)层 模型负责数据处理和业务逻辑,通常与数据库交互。创建一个基类模型,其他具体模型继承它。 class Model { protected $db;…

无限分类实现 php

无限分类实现 php

无限分类的实现方法 无限分类通常用于构建多层级结构的数据,如商品分类、部门架构等。以下是几种常见的实现方式: 邻接列表模式(Adjacency List) 邻接列表是最简单的实现方式,通过在每个记录…