Home > Backend Development > PHP Tutorial > How to generate dynamic verification code using PHP and GD library

How to generate dynamic verification code using PHP and GD library

WBOY
Release: 2023-06-25 14:18:01
Original
1098 people have browsed it

With the increasing emphasis on network security, dynamic verification codes have become a common tool for website registration, login and other operations. By repeatedly changing the verification code, malicious attacks by automated programs can be effectively prevented. As a popular back-end development language, PHP's built-in GD library provides common functions for generating and processing images, and can easily generate dynamic verification codes.

So, how to use PHP and GD library to generate dynamic verification code? Next, let us introduce it step by step.

1. Install the GD library

First make sure that the GD library has been installed on the server. If you are using a Linux system, you can install it through the terminal command:

sudo apt-get install php7.0-gd
Copy after login

Among them, 7.0 corresponds to the current PHP version, which can be adjusted according to your actual situation.

2. Generate verification code text

Next, you need to generate a random verification code text. You can use PHP's rand() function to generate a random string of numbers and letters, for example:

$code = '';
for ($i = 0; $i < 4; $i++) {
    $code .= chr(rand(65, 90));
}
Copy after login

In the above code, the chr() function converts the ASCII code Convert to the corresponding characters, so rand(65,90) means generating a random integer between 65 and 90, that is, characters between A and Z. A 4-digit verification code is generated here and can be adjusted as needed.

3. Create a canvas

Next you need to create a canvas to display the generated verification code. You can create a true color image of a specified size through the imagecreatetruecolor() function:

$width = 100;
$height = 50;
$image = imagecreatetruecolor($width, $height);
Copy after login

In the above code, a true color image with a canvas size of 100*50 pixels is set. If you need to change the canvas size, you can adjust it according to actual needs.

4. Set the background color

In order to make the verification code more beautiful, you can set the background color of the canvas through the imagecolorallocate() function. For example:

// 设定背景颜色
$bg_color = imagecolorallocate($image, 255, 255, 255);  // 白色
imagefill($image, 0, 0, $bg_color);
Copy after login

In the above code, the background color of the canvas is set to white.

5. Draw the verification code

Next, you need to draw each character of the generated verification code on the canvas. You can use the imagettftext() function to draw each character on the canvas:

// 描绘验证码
for ($i = 0; $i < 4; $i++) {
    $font_size = 20;
    $font_angle = rand(-20, 20);
    $font_x = 10 + $i * ($width - 20) / 4;
    $font_y = $height / 2 + $font_size / 2;
    $font_color = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
    imagettftext($image, $font_size, $font_angle, $font_x, $font_y, $font_color, 'arial.ttf', $code[$i]);
}
Copy after login

In the above code, each character in the verification code is drawn. Among them, $font_size represents the character size, $font_angle represents the character rotation angle, $font_x and $font_y represent the character's position in the canvas Position, $font_color represents the character color, arial.ttf represents the font file. Need to be adjusted according to actual needs.

6. Output the verification code

The last step is to output the generated verification code to the front-end page. You can use the header() function to set the image type, and then use the imagepng() function to output the image content:

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
Copy after login

In the above code, use header( ) function sets the image type, then uses the imagepng() function to output the image content to the page, and uses the imagedestroy() function to release the memory.

Through the above steps, you can easily use PHP and GD libraries to generate dynamic verification codes. Of course, in order to ensure security, corresponding verification mechanisms need to be added to the front-end page to prevent malicious attacks.

The above is the detailed content of How to generate dynamic verification code using PHP and GD library. For more information, please follow other related articles on the PHP Chinese website!

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