Home >Backend Development >PHP Tutorial >PHP Generate Image Verification Code Practice Notes_PHP Tutorial
PHP needs to use the PHP gd library and session to generate a graphical verification code. In this way, the gd library generates pictures for the user to see, and then the user input verification is submitted to the server and the value stored in the session for verification. Let’s take a look at the whole process. Process it.
Windows system GD library is enabled
Find extension=php_gd2.dll in the php.ini file and remove the previous “;”
Linux system GD library is enabled
##Check whether the GD library is installed or not using the command
php5 -m | grep -i gd
Or
php -i | grep -i --color gd
##If the GD library is not installed, install it on the server. The method is as follows
### If it is a source code installation, add the parameter
--with-gd
### If it is a Debian Linux system, use apt-get to install it, as follows
apt-get install php5-gd
### If it is a CentOS system, use yum to install it, as follows
yum install php-gd
### If it is a suse-based Linux system, use yast to install it, as follows
yast -i php5_gd
Okay, the php GD library is ready. Let’s look at an example of php generating a graphic verification code image
First of all, let me introduce to you the simple concept of verification code!
1. Verification code introduction
The verification code is a string of randomly generated numbers or symbols displayed on the page in the form of a picture. The user can identify the verification code information with the naked eye. During the submission operation, the characters on the picture need to be submitted at the same time. A function can only be used after the input submission verification is successful. If the characters submitted are different from those saved in the server session, the submitted information is considered invalid. In order to prevent automatic programs from analyzing and parsing pictures, some interferons are usually randomly generated on the picture or the characters are distorted, making automatic recognition more difficult. After the user submits, the verification code entered by the user is compared with the string saved in the session to achieve the verification effect. When the user submits the form, the page that receives the form checks whether the session generated by the server and the form value submitted by the client are consistent.
Consistent does not read or write to the database. Sessions allow a small amount of user information to be stored on the server; this information is temporary and will be automatically deleted when the user leaves the website.
2. PHP implementation process
PHP web page files are treated as general HTML web page files, and when editing, they can be written using the conventional method of editing HTML. Since PHP consumes very few system resources when used, has open source code, and is free, PHP has been used by more websites now. The following is the process of implementing verification code in PHP:
(1) Generate random numbers
Define the numbers and letters used to display on the image;
Loop and randomly select four defined letters and numbers;
The total number of characters obtained by connecting the numbers is four;
Save the generated numbers and letters, put the generated random numbers in the session variable, and compare them with the content submitted by the user in the future.
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< ? php
$ st r = a ' bcdef ghij k l mnopqrstuv wxyz 1234567890 '
|