Home >Backend Development >PHP Tutorial >Tips for generating simple graphical verification codes using PHP and GD library

Tips for generating simple graphical verification codes using PHP and GD library

WBOY
WBOYOriginal
2023-07-13 16:28:37804browse

Tips for generating simple graphical verification codes using PHP and GD libraries

With the development of the Internet, preventing malicious attacks and abuse has become an essential part of website development. CAPTCHA is a technical means widely used to verify user identity and prevent malicious robots from registering and logging in. As a popular server-side programming language, PHP, combined with the GD library, can quickly generate simple graphical verification codes.

1. Introduction to GD library
The GD library is an extension library of PHP. It provides a series of functions and methods for processing images. Through the GD library, functions such as image processing, image scaling, and image merging can be realized. In the generation of verification code, the GD library plays a vital role.

2. Generate a simple graphical verification code
Below, we will use a simple example to demonstrate how to generate a graphical verification code through PHP and GD libraries.

  1. Create a new PHP file named captcha.php.
  2. First, we need to generate a random verification code string. You can use the rand() function or mt_rand() function to generate a random number within a specified range and convert it into corresponding characters. For example, we can generate a captcha string containing 4 random letters and numbers.
$code = '';
for ($i = 0; $i < 4; $i++) {
    $code .= chr(mt_rand(65, 90));  // 生成大写字母
    $code .= chr(mt_rand(48, 57));  // 生成数字
}
  1. Next, we need to display the generated verification code string on the image. First, create an image of a specified size and set the background color and text color.
$image_width = 120;
$image_height = 40;
$image = imagecreatetruecolor($image_width, $image_height);
$background_color = imagecolorallocate($image, 255, 255, 255);  // 白色背景
$text_color = imagecolorallocate($image, 0, 0, 0);  // 黑色文本
  1. Draw the verification code string on the image. Text can be added to images using the imagettftext() function. Before using this function, please make sure you have downloaded a TrueType font file (.ttf). For example, we add text to the top left corner position.
$font_file = 'path/to/font.ttf';
$text_x = 10;
$text_y = 30;
imagettftext($image, 16, 0, $text_x, $text_y, $text_color, $font_file, $code);
  1. Finally, output the image and destroy the image resource.
header('Content-Type: image/png');  // 设置图像类型为PNG
imagepng($image);  // 输出图像
imagedestroy($image);  // 销毁图像资源

At this point, a simple graphic verification code has been generated successfully.

3. Apply graphic verification code
Embedding the generated verification code into the registration or login page of the website can effectively prevent the abuse of malicious robots. When users fill out the form, they need to also fill in the correct verification code to complete the registration or login operation.

<form action="register.php" method="post">
    <!-- 其他表单项 -->
    <img src="captcha.php" alt="验证码">
    <input type="text" name="captcha" placeholder="请填写验证码">
    <button type="submit">注册</button>
</form>

When processing a registration or login request on the backend, it is necessary to verify whether the verification code filled in by the user is consistent with the generated verification code.

$code = $_SESSION['captcha'];  // 从session中取出保存的验证码
$user_input = $_POST['captcha'];  // 用户填写的验证码
if ($user_input === $code) {
    // 验证码正确,继续处理注册或登录
} else {
    // 验证码错误,给出相应提示
}

4. Summary
Through PHP and GD library, we can easily generate simple graphical verification codes and apply them to the registration and login pages of the website. In this way, malicious robots can be effectively prevented from abusing the website. We can customize and optimize the style of the verification code according to our own needs, thereby improving the readability and security of the verification code.

The above is the detailed content of Tips for generating simple graphical verification codes using PHP and GD library. 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