How to generate verification code in PHP

WBOY
Release: 2023-09-24 11:30:01
Original
1702 people have browsed it

How to generate verification code in PHP

Title: How to generate verification code in PHP

In web development, verification code (captcha) is often used to prevent machine automation operations to improve the performance of the website safety. This article will introduce how to use PHP to generate verification codes and provide specific code examples.

1. The principle and process of generating verification code
The principle of generating verification code is to embed a randomly generated picture in the page, and display the generated verification code text and picture to the user. You need to enter the verification code correctly when submitting the form.

The process of generating a verification code is as follows:

  1. The server receives the request and starts generating the verification code;
  2. The server generates a random verification code text;
  3. The server generates the verification code text in the form of a picture;
  4. The server returns the generated verification code picture to the front-end page;
  5. The user enters the verification code in the front-end page;
  6. When the user submits the form, compare the verification code text with the verification code entered by the user.

2. Code example for generating verification code through PHP
The following is a simple code example that shows how to use PHP to generate verification code:

<?php
session_start();  // 开启session,用于保存验证码信息

// 生成验证码文本
$code = "";
for($i=0; $i<4; $i++) {
   $code .= chr(rand(97, 122));  // 生成随机小写字母
}

// 保存验证码文本到会话变量中
$_SESSION['code'] = $code;

// 绘制验证码图片
$width = 100;
$height = 40;
$image = imagecreatetruecolor($width, $height);

// 创建背景颜色
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width, $height, $bgColor);

// 生成验证码文字颜色
$textColor = imagecolorallocate($image, 0, 0, 0);

// 添加验证码文本到图片
imagettftext($image, 20, 0, 10, 30, $textColor, 'path/to/arial.ttf', $code);

// 添加干扰线
for ($i = 0; $i < 5; $i++) {
    $lineColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
}

// 设置响应头,告诉浏览器返回的是图片
header("Content-type: image/png");

// 输出图片到浏览器
imagepng($image);

// 销毁图片对象
imagedestroy($image);
?>
Copy after login

3. Use Generate Verification code

In the front-end page, you can use the following HTML code to display the generated verification code image and input box:

<img src="path/to/generate_captcha.php" alt="验证码">
<input type="text" name="captcha" placeholder="请输入验证码">
Copy after login

When the back-end PHP code handles form submission, you can use the following Code to verify whether the verification code entered by the user is correct:

<?php
session_start();

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

// 获取生成的验证码
$code = $_SESSION['code'];

// 验证验证码是否正确
if ($captcha === $code) {
    // 验证码正确,继续处理其他逻辑
} else {
    // 验证码错误,给出提示或进行其他处理
}
?>
Copy after login

The above code implements the function of generating a verification code in PHP and verifying the verification code when the form is submitted. You can customize and optimize the style, length, character set, etc. of the verification code as needed.

Summary:
This article introduces how to generate verification codes in PHP and provides specific code examples. The process of generating a verification code includes steps such as generating random verification code text, drawing a verification code picture, and saving the verification code information. Through the use of verification codes, the security of web applications can be improved and machine automation operations can be prevented.

The above is the detailed content of How to generate 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!