Home > Backend Development > PHP Tutorial > How to implement verification code in PHP

How to implement verification code in PHP

WBOY
Release: 2023-05-20 11:34:02
Original
6024 people have browsed it

With the continuous development of the Internet, more and more websites need to use verification codes to ensure security. Verification code is an authentication technology that relies on human capabilities and cannot be cracked by computers. It is widely used in website registration, login, password retrieval and other functions. The following will introduce how to use PHP to implement the verification code function.

1. Generate verification code image

The generation of verification code image is the core of the verification code function. It needs to generate a random character and render it as an image to display to the user. In PHP, you can use the GD library to generate images.

The GD library is a PHP extension for dynamically creating images. It provides a variety of functions that can be used to create images, modify images, save images and other operations. Verification code images can be easily created through the GD library.

The steps to generate the verification code are as follows:

  1. Set the image size, color and other attributes
  2. Generate a random string
  3. Render the string to
  4. Add interference lines, interference points, etc. to the image

The following is the specific code implementation:

<?php
//设置图片大小,这里是120x40
$image_width = 120;
$image_height = 40;

//创建画布
$image = imagecreatetruecolor($image_width, $image_height);

//设置背景色为白色
$bg_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bg_color);

//生成随机字符串
$code = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$charset_length = strlen($charset);

for ($i = 0; $i < 4; $i++) {
    $rand_index = rand(0, $charset_length - 1);
    $rand_char = substr($charset, $rand_index, 1);
    $code .= $rand_char;
}

//将字符串渲染到图像中
$font_size = 20;
$font_color = imagecolorallocate($image, 0, 0, 0);

$x = 10;
$y = ($image_height - $font_size) / 2;
for ($i = 0; $i < 4; $i++) {
    $char = substr($code, $i, 1);
    imagettftext($image, $font_size, rand(-10, 10), $x, $y, $font_color, "arial.ttf", $char);
    $x += $font_size + rand(-5, 5);
}

//添加干扰线、干扰点等
for ($i = 0; $i < 6; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, $image_width), rand(0, $image_height), rand(0, $image_width), rand(0, $image_height), $line_color);
}

for ($i = 0; $i < 50; $i++) {
    $point_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $image_width), rand(0, $image_height), $point_color);
}

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

//销毁图像
imagedestroy($image);
?>
Copy after login

The above code will generate a random verification code picture, and Display it in the browser. Among them, Arial font, black text, white background, 6 interference lines and 50 interference points are used. Can be customized as needed.

2. Verification of verification code

After generating the verification code image, you need to compare the verification code entered by the user with the generated verification code to confirm whether the user has entered the correct verification code. . In PHP, the generated verification code string can be stored in the session for subsequent verification.

The specific steps are as follows:

  1. Store the generated verification code string in the session
  2. User input verification code
  3. Verify user input Is the verification code the same as the verification code stored in the session?

The following is the code implementation:

<?php
//生成验证码
session_start();
$code = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$charset_length = strlen($charset);

for ($i = 0; $i < 4; $i++) {
    $rand_index = rand(0, $charset_length - 1);
    $rand_char = substr($charset, $rand_index, 1);
    $code .= $rand_char;
}

$_SESSION["captcha"] = $code;

//展示验证码图片
$image_width = 120;
$image_height = 40;
$image = imagecreatetruecolor($image_width, $image_height);
$bg_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bg_color);

$font_size = 20;
$font_color = imagecolorallocate($image, 0, 0, 0);

$x = 10;
$y = ($image_height - $font_size) / 2;
for ($i = 0; $i < 4; $i++) {
    $char = substr($code, $i, 1);
    imagettftext($image, $font_size, rand(-10, 10), $x, $y, $font_color, "arial.ttf", $char);
    $x += $font_size + rand(-5, 5);
}

header("Content-type:image/png");
imagepng($image);
imagedestroy($image);

//校验验证码
session_start();
if (isset($_POST["captcha"])) {
    $user_input = $_POST["captcha"];
    if (strcasecmp($user_input, $_SESSION["captcha"]) != 0) {
        echo "验证码错误";
    } else {
        echo "验证码正确";
    }
}
?>
Copy after login

In the above code, the session is used to store the generated verification code string, and Compare the entered verification code with the verification code stored in the session. Among them, the strcasecmp function is a function that compares two strings regardless of case. If they are the same, it returns 0, and if they are different, it returns a non-zero value.

Summary

Implementing the verification code function through PHP can effectively ensure the security of the website. When generating verification code images, you can easily create verification code images in various colors and fonts with the help of the GD library, and add interference lines, interference points, etc. to increase security. When verifying the verification code, the generated verification code string needs to be stored in the session for subsequent comparison.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template