How to generate refreshable image verification code using PHP
How to use PHP to generate a refreshable image verification code
With the development of the Internet, in order to prevent malicious attacks and automatic machine operations, many websites use verification code for user authentication. One common type of verification code is the image verification code, which generates a picture containing random characters and requires the user to enter the correct characters before proceeding.
This article will introduce how to use PHP to generate refreshable image verification codes and provide specific code examples.
Step 1: Create a verification code image
First, we need to create a function for generating a verification code image. Through PHP's GD library, we can easily manipulate images. The following is a sample code for creating a verification code image:
<?php function generateCaptcha($length = 6) { // 生成随机的验证码字符串 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $captcha = ''; for ($i = 0; $i < $length; $i++) { $captcha .= $chars[rand(0, strlen($chars) - 1)]; } // 将验证码字符串保存到Session中,以备后续验证 session_start(); $_SESSION['captcha'] = $captcha; // 创建一个空白的验证码图片 $image = imagecreatetruecolor(120, 40); $background_color = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $background_color); // 在验证码图片上绘制随机的字符 $text_color = imagecolorallocate($image, 0, 0, 0); imagettftext($image, 20, 0, 10, 30, $text_color, 'path/to/font.ttf', $captcha); // 输出验证码图片 header('Content-type: image/png'); imagepng($image); imagedestroy($image); } ?>
In the above code, the generateCaptcha
function is used to generate a verification code and save the verification code string to the Session. Then, create a blank verification code picture, set the background color, text color, font and other parameters, and draw the verification code on the picture. Finally, set the output content type to image through the header
function, call the imagepng
function to output the image, and then destroy the image resource.
Step 2: Use the verification code image
The following is a simple sample code for displaying the verification code image on the web page and accepting the verification code entered by the user:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>生成验证码</title> </head> <body> <form action="verify_captcha.php" method="post"> <label for="captcha">请输入验证码:</label> <input type="text" id="captcha" name="captcha"> <img src="/static/imghw/default1.png" data-src="captcha.php" class="lazy" alt="验证码"> <button type="submit">提交</button> </form> </body> </html>
In the above code, the action
attribute of the form
form points to a PHP file that verifies the verification code (such as verify_captcha.php
), img## The
src attribute of the #tag points to the PHP file that generates the verification code image (such as
captcha.php). After the user enters the verification code in the input box, the form is submitted and verified.
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $inputCaptcha = $_POST['captcha']; $captcha = $_SESSION['captcha']; if ($inputCaptcha === $captcha) { echo '验证码正确'; } else { echo '验证码错误'; } } ?>
session_start function, and then the verification code entered by the user and previously saved in the Session are obtained Verification codes are compared. If they are the same, output "verification code is correct"; if they are different, output "verification code is wrong".
The above is the detailed content of How to generate refreshable image verification code using PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
