当前位置:首页 > PHP

php登录验证码实现

2026-04-03 03:17:23PHP

PHP 登录验证码实现

生成验证码图片

使用 PHP 的 GD 库生成验证码图片,以下是一个简单的实现:

session_start();

// 创建空白图像
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);

// 设置背景颜色
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

// 生成随机验证码
$code = '';
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i < 5; $i++) {
    $code .= $chars[rand(0, strlen($chars) - 1)];
}

// 将验证码存入 session
$_SESSION['captcha'] = $code;

// 在图像上绘制验证码
$textColor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, 5, 30, 12, $code, $textColor);

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

在 HTML 表单中显示验证码

将生成的验证码图片嵌入到登录表单中:

php登录验证码实现

<form action="login.php" method="post">
    <label for="username">用户名:</label>
    <input type="text" id="username" name="username" required>

    <label for="password">密码:</label>
    <input type="password" id="password" name="password" required>

    <label for="captcha">验证码:</label>
    <input type="text" id="captcha" name="captcha" required>
    <img src="captcha.php" alt="CAPTCHA" onclick="this.src='captcha.php?'+Math.random();">

    <button type="submit">登录</button>
</form>

验证用户输入的验证码

在登录处理脚本中验证用户输入的验证码:

session_start();

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

    if (empty($userCaptcha) || strtolower($userCaptcha) !== strtolower($storedCaptcha)) {
        die('验证码错误');
    }

    // 验证码正确,继续处理登录逻辑
    // ...

    // 清除验证码 session
    unset($_SESSION['captcha']);
}

增强验证码安全性

为提高安全性,可以添加以下改进措施:

php登录验证码实现

// 添加干扰线
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);
}

// 添加噪点
for ($i = 0; $i < 100; $i++) {
    $pixelColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $width), rand(0, $height), $pixelColor);
}

// 使用字体文件而不是默认字体
$font = 'path/to/font.ttf';
imagettftext($image, 20, rand(-10, 10), 10, 30, $textColor, $font, $code);

使用 Composer 包实现

对于更复杂的需求,可以考虑使用现成的 Composer 包:

composer require gregwar/captcha

示例用法:

require 'vendor/autoload.php';

use Gregwar\Captcha\CaptchaBuilder;

$builder = new CaptchaBuilder;
$builder->build();

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

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

以上方法提供了从简单到复杂的 PHP 登录验证码实现方案,可以根据项目需求选择适合的实现方式。

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

相关文章

php实现购物车

php实现购物车

PHP实现购物车功能 购物车功能是电商网站的核心模块之一,通常涉及商品添加、删除、数量修改和结算等操作。以下是使用PHP实现购物车功能的几种方法。 使用Session存储购物车数据 Session是…

php实现socket

php实现socket

PHP 实现 Socket 通信 PHP 可以通过 socket 扩展实现网络通信,支持 TCP/UDP 协议。以下是实现 Socket 通信的核心步骤和代码示例。 创建 TCP 服务器 // 创建…

php实现上传

php实现上传

PHP 文件上传实现 HTML 表单设置 创建一个包含文件上传字段的 HTML 表单,确保表单的 enctype 属性设置为 multipart/form-data,这是文件上传必需的设置。 <…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数: $q…

php 实现评论

php 实现评论

实现 PHP 评论功能 数据库设计 创建数据库表存储评论数据,通常包含以下字段: id:评论唯一标识 user_id:用户ID content:评论内容 created_at:评论时间 parent…

php实现域名

php实现域名

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