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

如何使用PHP函数实现验证码功能

发布时间:2023-06-05 07:28:11

验证码是一个常见的安全功能,它用于验证用户是否为人类,避免机器恶意攻击。在网络安全中,验证码被广泛应用于用户注册、登录、密码重置等场景,以防止黑客攻击等安全风险。在PHP中,可以方便地使用函数来实现验证码功能。本文将介绍如何使用PHP函数实现验证码功能,包括生成验证码、显示验证码、验证验证码等步骤。

1. 生成验证码

生成验证码是实现验证码功能的 步。PHP中可以使用imagecreate()函数来创建一张图片,使用imagecolorallocate()函数来为图片分配颜色,使用imagestring()函数来向图片中输出字符串。具体实现步骤如下:

(1)创建一张空白图片:

$image = imagecreate($width, $height);

其中,$width为图片宽度,$height为图片高度。

(2)为图片分配颜色:

$bgcolor = imagecolorallocate($image, 255, 255, 255);

$linecolor = imagecolorallocate($image, 200, 200, 200);

$textcolor = imagecolorallocate($image, 0, 0, 0);

其中,$bgcolor为图片背景色,$linecolor为干扰线颜色,$textcolor为验证码文字颜色。

(3)向图片中输出字符串:

imagestring($image, 5, $x, $y, $code, $textcolor);

其中,$x、$y为字符串位置,$code为验证码文本内容。

(4)添加干扰线:

imageline($image, $x1, $y1, $x2, $y2, $linecolor);

其中,$x1、$y1、$x2、$y2为干扰线起始和结束位置。

(5)将验证码图片保存到本地:

imagepng($image, $path);

其中,$path为要保存的图片路径。

完整代码如下:

function create_captcha($width = 100, $height = 30, $length = 4, $path = 'captcha.png')

{

    $code = '';

    for ($i = 0; $i < $length; $i++) {

        $code .= chr(rand(65, 90));

    }

    $image = imagecreate($width, $height);

    $bgcolor = imagecolorallocate($image, 255, 255, 255);

    $linecolor = imagecolorallocate($image, 200, 200, 200);

    $textcolor = imagecolorallocate($image, 0, 0, 0);

    for ($i = 0; $i < 5; $i++) {

        imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $linecolor);

    }

    $font = 'arial.ttf';

    for ($i = 0; $i < $length; $i++) {

        imagettftext($image, 18, rand(-15, 15), $i * $width / $length, $height / 2 + 9, $textcolor, $font, $code[$i]);

        //imagestring($image, 5, $i * $width / $length + 5, rand(0, $height / 2), $code[$i], $textcolor);

    }

    imagepng($image, $path);

    imagedestroy($image);

    return $code;

}

其中,$width、$height为图片宽高,$length为验证码长度,$path为保存路径。

2. 显示验证码

生成验证码之后,需要在页面上显示验证码,以让用户输入。可以通过HTML的<img>标签来显示验证码图片,代码如下:

<img src="captcha.png" alt="captcha" />

其中,captcha.png为验证码图片路径,需要用上一步中生成的图片路径。

3. 验证验证码

用户输入验证码之后,需要验证验证码的正确性。因为验证码是随机生成的,所以只有使用相同的算法才能验证其正确性。可以根据用户输入的验证码和生成验证码的算法来进行验证。代码如下:

function validate_captcha($code)

{

    session_start();

    $captcha = $_SESSION['captcha'];

    return strtoupper($code) == strtoupper($captcha);

}

其中,$_SESSION['captcha']为生成的验证码,需要在验证前获取。uppercase()函数用于将字符串转换为大写,以实现大小写不敏感验证。

完整验证码实现代码如下:

<?php

session_start();

if (isset($_POST['submit'])) {

    if (validate_captcha($_POST['code'])) {

        echo "验证码正确!";

    } else {

        echo "验证码错误!";

    }

}

$captcha = create_captcha();

$_SESSION['captcha'] = $captcha;

?>

<form method="post">

    <img src="captcha.png" alt="captcha" /><br />

    <input type="text" name="code" /><br />

    <input type="submit" name="submit" value="提交" />

</form>

<?php

function create_captcha($width = 100, $height = 30, $length = 4, $path = 'captcha.png')

{

    $code = '';

    for ($i = 0; $i < $length; $i++) {

        $code .= chr(rand(65, 90));

    }

    $image = imagecreate($width, $height);

    $bgcolor = imagecolorallocate($image, 255, 255, 255);

    $linecolor = imagecolorallocate($image, 200, 200, 200);

    $textcolor = imagecolorallocate($image, 0, 0, 0);

    for ($i = 0; $i < 5; $i++) {

        imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $linecolor);

    }

    $font = 'arial.ttf';

    for ($i = 0; $i < $length; $i++) {

        imagettftext($image, 18, rand(-15, 15), $i * $width / $length, $height / 2 + 9, $textcolor, $font, $code[$i]);

        //imagestring($image, 5, $i * $width / $length + 5, rand(0, $height / 2), $code[$i], $textcolor);

    }

    imagepng($image, $path);

    imagedestroy($image);

    return $code;

}

function validate_captcha($code)

{

    session_start();

    $captcha = $_SESSION['captcha'];

    return strtoupper($code) == strtoupper($captcha);

}

?>

本文介绍了PHP函数实现验证码功能的步骤,包括生成验证码、显示验证码、验证验证码等。验证码是一项重要的安全功能,在实际开发中需要注意其安全性和可靠性。