Php creates a dynamic random verification code _ php instance-php Tutorial

Source: Internet
Author: User
Tags image identifier
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.