当前位置:首页 > PHP

php实现验证码

2026-04-02 19:19:15PHP

使用 PHP 生成验证码

验证码是一种用于区分人类用户和机器人的安全机制。以下是使用 PHP 生成验证码的几种方法。

使用 GD 库生成图片验证码

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

<?php
session_start();

// 创建画布
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);

// 设置背景色和文字颜色
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);

// 填充背景
imagefill($image, 0, 0, $bgColor);

// 生成随机验证码
$code = substr(md5(uniqid(rand(), true)), 0, 6);
$_SESSION['captcha'] = $code;

// 在画布上绘制验证码
imagestring($image, 5, 30, 12, $code, $textColor);

// 输出图像
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>

添加干扰元素

为了提高安全性,可以添加干扰线或干扰点。

<?php
session_start();

$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);

$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);

imagefill($image, 0, 0, $bgColor);

$code = substr(md5(uniqid(rand(), true)), 0, 6);
$_SESSION['captcha'] = $code;

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

imagestring($image, 5, 30, 12, $code, $textColor);

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

验证用户输入

生成验证码后,需要验证用户输入是否正确。

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $userCaptcha = $_POST['captcha'] ?? '';
    $validCaptcha = $_SESSION['captcha'] ?? '';

    if ($userCaptcha === $validCaptcha) {
        echo "验证码正确";
    } else {
        echo "验证码错误";
    }
}
?>

使用 Composer 包

可以使用第三方库如 gregwar/captcha 简化验证码生成。

安装:

composer require gregwar/captcha

使用:

<?php
require 'vendor/autoload.php';

session_start();

$builder = new Gregwar\Captcha\CaptchaBuilder;
$builder->build();

$_SESSION['captcha'] = $builder->getPhrase();

header('Content-Type: image/jpeg');
$builder->output();
?>

前端集成

在 HTML 表单中显示验证码并提交验证。

php实现验证码

<form method="post" action="validate.php">
    <img src="captcha.php" alt="CAPTCHA" onclick="this.src='captcha.php?'+Math.random();">
    <input type="text" name="captcha" required>
    <button type="submit">提交</button>
</form>

注意事项

  • 验证码应存储在会话中,避免直接暴露在客户端。
  • 定期更换验证码,防止重复使用。
  • 结合其他安全措施如 IP 限制,提高安全性。

通过以上方法,可以轻松实现 PHP 验证码功能,有效防止自动化攻击。

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

相关文章

php实现文件上传

php实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

php实现图片上传

php实现图片上传

实现图片上传的基本步骤 创建HTML表单用于文件上传,表单需设置enctype="multipart/form-data"属性,确保文件数据正确传输。 <form action="upload…

php购物车实现

php购物车实现

PHP购物车实现方法 使用PHP实现购物车功能可以通过多种方式完成,以下是几种常见的实现方法: Session存储购物车数据 利用PHP的$_SESSION超全局变量存储购物车数据是最简单的方法。用…

php实现统计

php实现统计

PHP 实现统计功能 在 PHP 中实现统计功能通常涉及数据的收集、处理和展示。以下是一些常见的统计实现方法: 数据库数据统计 通过 SQL 查询可以直接从数据库中获取统计结果: $conn =…

无限分类实现 php

无限分类实现 php

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

php实现域名

php实现域名

PHP实现域名操作的方法 获取当前域名 使用$_SERVER['HTTP_HOST']可以获取当前访问的域名: $currentDomain = $_SERVER['HTTP_HOST']; ech…