This article describes how to use php to create a dynamic random Verification Code. For more information, see CAPTCHA) is the abbreviation of "Completely automation Public Turing test to tell Computers and Humans Apart" (a Completely Automated Turing test that distinguishes Computers from Humans, it is a fully-automated public program that distinguishes a user from a computer or a person. It can prevent malicious password cracking, ticket flushing, and Forum bumping, effectively preventing a hacker from continuously logging on to a specific registered user using brute force cracking methods of specific programs, in fact, the verification code is now used by many websites. We have implemented this function in a relatively simple way.
This question can be generated and judged by computers, but it must be answered by humans. Because the computer cannot answer CAPTCHA's questions, users who answer questions can be considered as humans.
Php makes a dynamic verification code based on php image processing. The following describes the php image processing.
I. Introduction to php Image Processing
In PHP5, processing of dynamic images is much easier than before. PHP5 contains 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 in PHP5 is the upgraded GD2 library, which contains some useful JPG functions that support true color image processing.
Generally, generated images are stored in the PHP document format, but dynamic images can be directly obtained through the HTML image Insertion Method SRC. For example, verification code, watermark, and thumbnail.
General process for creating images:
1) set the header to tell the browser the MIME type you want to generate.
2) create an image region based on which operations will be performed in the future.
3) Fill the background in the blank image area.
4) Draw the input text of the graphic outline on the background.
5). Output the final image.
6) Clear all resources.
7). Call images on other pages.
Step 1: Set the file MIME type, and change the output type to image stream.
The Code is as follows:
Header ('content-Type: image/png ;');
Generally, the generated image can be png, jpeg, gif, or wbmp.
Step 2: Create a graphic area and image background
Imagecreatetruecolor () returns an image identifier, representing a black image of x_size and y_size. Syntax: resource imagecreatetruecolor (int $ width, int $ height)
The Code is as follows:
$ Im = imagecreatetruecolor (200,200 );
Step 3: Draw a filling background in the blank image area
Color filler; imagecolorallocate -- assign color to an image; Syntax: int imagecolorallocate (resource $ image, int $ red, int $ green, int $ blue)
The Code is as follows:
$ Blue = imagecolorallocate (USD im, 0,102,255 );
Fill the blue color to the background; imagefill -- fill the area; Syntax: bool imagefill (resource $ image, int $ x, int $ y, int $ color)
The Code is as follows:
Imagefill ($ im, 0, 0, $ blue );
Step 4: Enter some lines and text on the blue background
Color Filler
The Code is as follows:
$ White = imagecolorallocate ($ im, 255,255,255 );
Draw two line segments: imageline
Imageline () uses color to draw a line segment from the coordinates x1, y1 to x2, y2 (0 in the upper left corner of the image. Syntax: bool imageline (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color)
The Code is as follows:
Imageline ($ im, 0, 0, 200,200, $ white );
Imageline ($ im, 200,0, 0,200, $ white );
Draw a line of string horizontally: imagestring
Imagestring () uses the col color to draw string s to the x and y coordinates of the 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 the 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:
Imagestring ($ im, 5, 66, 20, 'jingwhale ', $ white );
Step 5: output the final image
Imagepng () outputs the GD image stream (image) in PNG format to the standard output (usually in a browser), or outputs the file name to the file if filename is used. Syntax: bool imagepng (resource $ image [, string $ filename])
The Code is as follows:
Imagepng ($ im );
Step 6: I want to clear all the resources
Imagedestroy () releases the memory associated with the image. Syntax: bool imagedestroy (resource $ image)
The Code is as follows:
Imagedestroy ($ im );
Images Created by calling other pages (html)
The Code is as follows:
The sample code is as follows:
The Code is as follows:
<? Php
// Step 1: Set the object MIME type
Header ('content-Type: image/png ;');
// Step 2: Create a graphic area with an image background
$ Im = imagecreatetruecolor (200,200 );
// Step 3: Draw the filling background in the blank image area
$ Blue = imagecolorallocate (USD im, 0,102,255 );
Imagefill ($ im, 0, 0, $ blue );
// Step 4: Enter some lines and text on the blue background
$ White = imagecolorallocate ($ im, 255,255,255 );
Imageline ($ im, 0, 0, 200,200, $ white );
Imageline ($ im, 200,0, 0,200, $ white );
Imagestring ($ im, 5, 66, 20, 'jing. Whale ', $ white );
// Step 5: output the final image
Imagepng ($ im );
// Step 6: clear all the resources
Imagedestroy ($ im );
?>
Display Effect:
2. Create a dynamic verification code
Appendix: code source address https://github.com/cnblogs-/php-captcha
1. Create an image with a verification code and blur the background.
The random code is in hexadecimal notation. blur the background to add lines and snow to the image background.
1) create a random code
The Code is as follows:
For ($ I = 0; $ I <$ _ rnd_code; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}
String dechex (int $ number), returns a string containing the hexadecimal representation of the given number parameter.
2) Save in session
The Code is as follows:
$ _ SESSION ['code'] =$ _ nms
3) create an image
The Code is as follows:
// Create an image
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );
// White
$ _ White = imagecolorallocate ($ _ img, 255,255,255 );
// Fill
Imagefill ($ _ img, 0, 0, $ _ white );
If ($ _ flag ){
// Black, border
$ _ Black = imagecolorallocate ($ _ img, 0, 0 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ black );
}
4) blur background
The Code is as follows:
// Draw 6 lines randomly
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imageline ($ _ img, mt_rand (0, $ _ width), mt_rand (0, $ _ height), mt_rand (0, $ _ width), mt_rand (0, $ _ height), $ _ rnd_color );
}
// Random snowflake
For ($ I = 0; I I <100; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ height), '*', $ _ rnd_color );
}
5) Output and destruction
The Code is as follows:
// Output the verification code
For ($ I = 0; $ I $ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 ));
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], $ _ rnd_color );
}
// Output image
Header ('content-Type: image/png ');
Imagepng ($ _ img );
// Destroy
Imagedestroy ($ _ img );
Encapsulate it in the global. func. php global function library. The function name is _ code () for calling. We will set four parameters $ _ width, $ _ height, $ _ rnd_code, and $ _ flag to improve the flexibility of functions.
* @ Param int $ _ width: length of the Verification Code: 75 + 50 is recommended for 6 characters. For 8 characters, 75 + 50 + 50 is recommended, and so on.
* @ Param int $ _ height of the Verification Code
* @ Param int $ _ number of digits of the rnd_code Verification Code
* @ Param bool $ _ border required for flag verification code: true with border, false without border (default)
The encapsulated code is as follows:
The Code is as follows:
<? Php
/**
* [Verification-code] (C) 2015-2100 jingwhale.
*
* This is a freeware
* $ Id: global. func. php 2015-02-05 20:53:56 jingwhale $
*/
/**
* _ Code () is a verification code function.
* @ Access public
* @ Param int $ _ width: length of the Verification Code: 75 + 50 is recommended for 6 characters. For 8 characters, 75 + 50 + 50 is recommended, and so on.
* @ Param int $ _ height of the Verification Code
* @ Param int $ _ number of digits of the rnd_code Verification Code
* @ Param bool $ _ border required for flag verification code: true with border, false without border (default)
* @ Return void: A verification code is generated after the function is executed.
*/
Function _ code ($ _ width = 75, $ _ height = 25, $ _ rnd_code = 4, $ _ flag = false ){
// Create a random code
For ($ I = 0; $ I <$ _ rnd_code; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}
// Save it in session
$ _ SESSION ['code'] =$ _ nmsg;
// Create an image
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );
// White
$ _ White = imagecolorallocate ($ _ img, 255,255,255 );
// Fill
Imagefill ($ _ img, 0, 0, $ _ white );
If ($ _ flag ){
// Black, border
$ _ Black = imagecolorallocate ($ _ img, 0, 0 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ black );
}
// Draw 6 lines.
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imageline ($ _ img, mt_rand (0, $ _ width), mt_rand (0, $ _ height), mt_rand (0, $ _ width), mt_rand (0, $ _ height), $ _ rnd_color );
}
// Snowflake
For ($ I = 0; I I <100; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ height), '*', $ _ rnd_color );
}
// Output the verification code
For ($ I = 0; $ I $ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 ));
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], $ _ rnd_color );
}
// Output image
Header ('content-Type: image/png ');
Imagepng ($ _ img );
// Destroy
Imagedestroy ($ _ img );
}
?>
2. Create a verification mechanism
Create a php verification page and use session to check whether the verification code is consistent.
1) Create verification-code.php verification page
The Code is as follows:
<? Php
/**
* [Verification-code] (C) 2015-2100 jingwhale.
*
* This is a freeware
* $ Id: verification-code.php 20:53:56 jingwhale $
*/
// Set character set encoding
Header ('content-Type: text/html; charset = UTF-8 ');
?>
Verification code
Shown as follows:
2) create a verification code image page
Create codeimg. php to provide verification code image for img in verification-code.php html code
First, you must enable the session on the codeimg. php page;
Secondly, we will introduce the encapsulated global. func. php global function library;
Finally, run _ code ();
The Code is as follows:
<? Php
/**
* [Verification-code] (C) 2015-2100 jingwhale.
*
* This is a freeware
* $ Id: codeimg. php 2015-02-05 20:53:56 jingwhale $
*/
// Enable session
Session_start ();
// Introduce the global function library (custom)
Require dirname (_ FILE _). '/regiondes/global. func. php ';
// Run the verification code function. You can use the _ code method of the database to set various attributes of the Verification code and generate images.
_ Code (125,25, 6, false );
?>
3) create a session check mechanism
First, you must also enable session on the verification-code.php page;
Secondly, the verification Code is designed to be submitted. This document submits the verification code in get mode. When action = verification, the verification code is submitted successfully;
Finally, create a verification function to verify the Verification Code submitted by the client user with the server codeimg. check whether the session verification code in php is consistent. Here is a js pop-up function _ alert_back (), which is also encapsulated in global. func. in php;
Modify the php code in the verification-code.php as follows:
The Code is as follows:
<? Php
/**
* [Verification-code] (C) 2015-2100 jingwhale.
*
* This is a freeware
* $ Id: verification-code.php 20:53:56 jingwhale $
*/
// Set character set encoding
Header ('content-Type: text/html; charset = UTF-8 ');
// Enable session
Session_start ();
// Introduce the global function library (custom)
Require dirname (_ FILE _). '/regiondes/global. func. php ';
// Verify the verification code
If ($ _ GET ['action'] = 'verification '){
If (! ($ _ POST ['code'] ==$ _ SESSION ['code']) {
_ Alert_back ('the verification code is incorrect! ');
} Else {
_ Alert_back ('verification Code passed! ');
}
}
?>
Verification code