This article mainly introduces the relevant information on the method of producing dynamic random verification code with php. Friends who need it can refer to it
CAPTCHA is the abbreviation of "Completely Automated Public Turing test to tell Computers and Humans Apart". It is a public fully automated test to distinguish whether a user is a computer or a human. program. It can prevent: malicious cracking of passwords, ticket fraud, forum flooding, and effectively prevents a hacker from using a specific program to brute force a specific registered user to continuously log in. In fact, using verification codes is a common method for many websites now. We use This function is implemented in a relatively simple way.
This question can be generated and judged by a computer, but only a human can answer it. Since computers cannot answer CAPTCHA questions, the user who answers the questions can be considered a human.
Php generates dynamic verification codes based on PHP image processing. Let’s first introduce PHP image processing.
1.Introduction to php image processing
In PHP5, processing dynamic images is much easier than before. PHP5 includes the GD extension package in the php.ini file. You only need to remove the corresponding comments of the GD extension package to use it normally. The GD library included in PHP5 is the upgraded GD2 library, which contains some useful JPG functions that support true color image processing.
Generally generated graphics are stored in PHP document format, but dynamic graphics can be directly obtained through HTML image insertion method SRC. For example, verification code, watermark, thumbnail, etc.
General process for creating images:
1). Set the header to tell the browser the MIME type you want to generate.
2). Create an image area, and all subsequent operations will be based on this image area.
3). Draw a filled background in the blank image area.
4). Draw graphic outlines on the background to enter text.
5). Output the final graphics.
6). Clear all resources.
7). Call images from other pages.
The first step is to set the file MIME type and output type. Change the output type to image stream
The code is as follows:
Generally generated images can be png, jpeg, gif, wbmp
The second step is to create a graphics area and image background
imagecreatetruecolor() returns an image identifier representing a black image of size x_size and y_size. Syntax: resource imagecreatetruecolor ( int $width , int $height )
The code is as follows:
The third step is to draw a filled background in the blank image area
Requires a color filler; imagecolorallocate -- assigns a color to an image; syntax: int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
The code is as follows:
Fill this blue color into the background; imagefill -- area filling; syntax: bool imagefill ( resource $image , int $x , int $y , int $color )
The code is as follows:
The fourth step is to enter some lines, text, etc. on the blue background
Color Filler
The code is as follows:
Draw two line segments: imageline
imageline() draws a line segment in the image image from coordinates x1, y1 to x2, y2 (the upper left corner of the image is 0, 0) using color color. Syntax: bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
The code is as follows:
Draw a line of string horizontally: imagestring
imagestring() uses col color to draw the string s to the x, y coordinates of the image represented by image (this is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0). If font is 1, 2, 3, 4 or 5, the built-in font is used. Syntax: bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
The code is as follows:
Step 5, output the final graphics
imagepng() Outputs a GD image stream (image) in PNG format to standard output (usually a browser), or to a file if filename is given. Syntax: bool imagepng ( resource $image [, string $filename ] )
The code is as follows:
The sixth step is to clear all resources
imagedestroy() releases the memory associated with image. Syntax: bool imagedestroy ( resource $image )
The code is as follows:
Graphics created by calling other pages (html)
The code is as follows:
The sample code is as follows:
The code is as follows:
Display effect:
2. Create dynamic verification code
Attachment: Code source address https://github.com/cnblogs-/php-captcha
1. Create a picture with verification code and blur the background
The random code uses hexadecimal; the blurred background means adding lines, snowflakes, etc. to the background of the picture.
1) Create random code
The code is as follows:
2) Save in session
The code is as follows:
3) Create pictures
The code is as follows:
4) Blurred background
The code is as follows:
5) Output and destruction
The code is as follows:
Encapsulate it in the global.func.php global function library, and the function name is _code() for easy calling. We will set the four parameters $_width, $_height, $_rnd_code, $_flag to enhance the flexibility of the function.
* @param int $_width The length of the verification code: if you want 6 digits, 75+50 is recommended; if you want 8 digits, 75+50+50 is recommended, and so on
* @param int $_height The height of the verification code
* @param int $_rnd_code The number of digits in the verification code
* @param bool $_flag Whether the verification code requires a border: true with border, false without border (default)
The encapsulated code is as follows:
The code is as follows:
2.创建验证机制
创建php验证页面,通过session来检验验证码是否一致。
1)创建verification-code.php验证页面
代码如下:
显示如下:
2)创建产生验证码图片页面
创建codeimg.php为verification-code.php html代码里的img提供验证码图片
首先必须在codeimg.php页面开启session;
其次,将我们封装好的global.func.php全局函数库引入进来;
最后,运行_code();
代码如下:
3)创建session检验机制
首先必须在verification-code.php页面也开启session;
其次,设计提交验证码的方式,本文以get方式提交,当action=verification时提交成功;
最后,创建验证函数,原理是将客户端用户提交的验证码同服务器codeimg.php中session的验证码是否一致;这里有一个js弹窗函数_alert_back(),我们也把它封装在global.func.php里;
修改verification-code.php中php代码如下:
代码如下:
3. Click on the verification code image to update the verification code
If you want to update the verification code above, you must refresh the page; we write a codeimg.js function to update the verification code by clicking on the verification code image
The code is as follows:
Then it in the verification-code.php html code head.