欢迎访问宙启技术站
智能推送

利用PHP函数生成验证码功能

发布时间:2023-06-22 10:28:29

验证码(CAPTCHA)是一种用来区分人类用户和机器程序的防机器人攻击技术。一般通过在表单中增加验证码的方式,要求用户在提交表单前先输入验证码,以确保目标网站不受到恶意机器人的攻击。

在PHP中生成验证码比较简单,下面我们来看看如何实现。

一、生成验证码的原理

验证码的生成一般分为以下几个步骤:

1. 生成随机字符串。

2. 随机选取一种字体。

3. 设置背景颜色和字体颜色。

4. 在图片上绘制字符。

5. 添加噪点干扰。

6. 生成图片并输出到浏览器中。

二、编写验证码生成函数

1. 生成随机字符串

生成随机字符串可以使用PHP的rand函数或mt_rand函数,这里我们使用mt_rand函数。

function randomString($length) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}

2. 选取字体

PHP可以通过imageloadfont函数加载字体文件,并通过imagefontwidth和imagefontheight函数获取字体的宽度和高度信息。

$fontfile = "tahoma.ttf";
$fontsize = 24;
$fontwidth = imagefontwidth($fontsize);
$fontheight = imagefontheight($fontsize);
$fontfile = "tahoma.ttf";

3. 设置背景颜色和字体颜色

这里我们随机生成一种背景颜色和一种字体颜色。

$bgcolor = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
$fontcolor = imagecolorallocate($img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));

4. 绘制字符

通过imagettftext函数,在图片上绘制随机字符串。

$text = randomString(4);
$x = mt_rand(10, 20);
for ($i = 0; $i < strlen($text); $i++) {
    $angle = mt_rand(-30, 30);
    imagettftext($img, $fontsize, $angle, $x, $fontheight + mt_rand(-5, 5), $fontcolor, $fontfile, substr($text, $i, 1));
    $x += $fontwidth + mt_rand(-5, 5);
}

5. 添加噪点干扰

通过imageellipse和imagesetpixel函数,在图片上添加一些随机的点和圆来增加验证码的难度。

for ($i = 0; $i < 10; $i++) {
    imageellipse($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(5, 20), mt_rand(5, 20), $bgcolor);
}
for ($i = 0; $i < 100; $i++) {
    imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $bgcolor);
}

6. 生成图片并输出

通过imagejpeg函数将图片输出到浏览器中。

header("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);

我们将以上代码整合为一个函数,完整代码如下:

function generateCaptcha() {
    $width = 150;
    $height = 50;

    $img = imagecreatetruecolor($width, $height);
    $bgcolor = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
    imagefill($img, 0, 0, $bgcolor);

    $fontfile = "tahoma.ttf";
    $fontsize = 24;
    $fontwidth = imagefontwidth($fontsize);
    $fontheight = imagefontheight($fontsize);
    $fontfile = "tahoma.ttf";

    $fontcolor = imagecolorallocate($img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));

    $text = randomString(4);
    $x = mt_rand(10, 20);
    for ($i = 0; $i < strlen($text); $i++) {
        $angle = mt_rand(-30, 30);
        imagettftext($img, $fontsize, $angle, $x, $fontheight + mt_rand(-5, 5), $fontcolor, $fontfile, substr($text, $i, 1));
        $x += $fontwidth + mt_rand(-5, 5);
    }

    for ($i = 0; $i < 10; $i++) {
        imageellipse($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(5, 20), mt_rand(5, 20), $bgcolor);
    }
    for ($i = 0; $i < 100; $i++) {
        imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $bgcolor);
    }

    header("Content-type: image/jpeg");
    imagejpeg($img);
    imagedestroy($img);

    return $text;
}

function randomString($length) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}

三、使用验证码生成函数

使用验证码生成函数很简单,只需要在需要显示验证码的页面上调用该函数即可。

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

$_SESSION["captcha"] = generateCaptcha();
?>

<form method="post">
    <img src="captcha.php">
    <br>
    <input type="text" name="captcha" maxlength="4">
    <br>
    <input type="submit" name="submit">
</form>

在上面的代码中,我们使用session来保存验证码的值,并在表单提交时检查用户输入的值是否与验证码相等。

注意:在使用验证码时,必须确保在所有的session操作之前没有输出内容,否则session操作将失效,导致无法验证验证码的值。

四、总结

通过以上的介绍,我们可以看出,使用PHP函数生成验证码非常简单,只要理解验证码生成的原理,并掌握PHP图像处理函数的使用,就可以轻松编写自己的验证码程序。而使用验证码可以有效地保护我们的web应用程序不受恶意攻击的侵害,提高了web应用程序的安全性和可靠性。