Home  >  Article  >  Backend Development  >  PHP development skills: How to implement the verification code function

PHP development skills: How to implement the verification code function

WBOY
WBOYOriginal
2023-09-21 13:16:412108browse

PHP development skills: How to implement the verification code function

PHP development skills: How to implement the verification code function

Verification code (CAPTCHA) is a common technology used to verify user identity and is used in website development. widely used. Verification codes can effectively prevent malicious registration by robots, brute force cracking and other malicious behaviors, and improve the security of the website. This article will introduce how to use PHP language to implement the verification code function and provide specific code examples.

1. Generate verification code

Generating verification code is the first step to realize the verification code function. We can create pictures through the GD library and draw randomly generated verification code characters on the pictures. The following is a sample code for generating a verification code:

<?php
session_start();

// 随机生成验证码字符
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$randomString = '';
$length = 6;

for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, strlen($characters) - 1)];
}

// 将验证码字符保存到session中
$_SESSION['captcha'] = $randomString;

// 创建验证码图片
$image = imagecreatetruecolor(200, 100);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);

imagefilledrectangle($image, 0, 0, 200, 100, $bgColor);
imagettftext($image, 40, 0, 60, 60, $textColor, 'font.ttf', $randomString);

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

In the above code, first we use the session_start() function to open the session to store the verification code characters. We then randomly select characters through the loop, save them into the variable $randomString, and store them in $_SESSION['captcha']. Next, use the imagecreatetruecolor() function to create a canvas $image with a size of 200x100 pixels, and set the background color and text color. Use the imagefilledrectangle() function to fill the canvas with white, and then use the imagettftext() function to draw the captcha characters on the canvas. Finally, use the header() function to set the response type to image/png, and use the imagepng() function to output the canvas as a PNG image.

2. Verification code verification

After generating the verification code, we need to display it on the web page and provide an input box for the user to enter the verification code. When the user submits the form, we need to verify the correctness of the verification code. The following is a sample code for verification code verification:

<?php
session_start();

// 获取用户输入的验证码
$userCaptcha = $_POST['captcha'];

// 验证码比较(不区分大小写)
if (strcasecmp($userCaptcha, $_SESSION['captcha']) == 0) {
    echo '验证码正确';
} else {
    echo '验证码错误';
}

// 清除session中的验证码
unset($_SESSION['captcha']);
?>

In the above code, first we use the session_start() function to open the session, and then obtain the verification code entered by the user from the form submitted by the user $_POST['captcha' ]. Then, use the strcasecmp() function to compare the verification code entered by the user with the verification code stored in the session. The strcasecmp() function is a case-insensitive string comparison function that returns 0 to indicate equality and non-zero to indicate inequality. Output corresponding information based on the comparison results. Finally, use the unset() function to clear the verification code in the session for next time use.

3. Use the verification code function

The following is a sample code for using the verification code function:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>验证码示例</title>
</head>
<body>
    <form action="verify.php" method="POST">
        <img src="captcha.php" alt="验证码">
        <input type="text" name="captcha" placeholder="请输入验证码">
        <input type="submit" value="提交">
    </form>
</body>
</html>

In the above code, we used PHP development skills: How to implement the verification code function

Summary

This article introduces how to use PHP language to implement the verification code function and provides specific code examples. By generating random characters and drawing verification code images, we can effectively prevent attacks by bots and malicious actors on the website. When using the verification code function, you need to pay attention to protecting the security of the verification code characters to prevent them from being guessed by malicious programs. I hope this article can help you understand and apply the verification code function.

The above is the detailed content of PHP development skills: How to implement the verification code function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn