Complete PHP graphic verification code program source code!

Source: Internet
Author: User
Complete PHP graphic verification code program source code !,

The code is as follows:
/* Website verification code program
* Running environment: PHP5.0.18 is successfully debugged.
* Gd2 graphics library is required (php_gd2.dll is enabled in PHP. INI)
* File name: showimg. php
* Author: 17php.com
* Time: 2007.03
* Technical support: www.17php.com
*/

// Generate a 4-digit verification code randomly
$ Num = "";
For ($ I = 0; $ I <4; $ I ++ ){
$ Num. = rand (0, 9 );
}
// The four-digit verification code can also be directly generated using rand (,)
// Write the generated verification code to the session, which is used on the standby verification page.
Session_start ();
$ _ SESSION ["Checknum"] = $ num;
// Create an image and define the color value
Header ("Content-type: image/PNG ");
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (60, 20 );
$ Black = ImageColorAllocate ($ im, 0, 0 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
Imagefill ($ im, 0, 0, $ gray );

// Randomly draw two dotted lines to interfere
$ Style = array ($ black, $ gray, $ gray );
Imagesetstyle ($ im, $ style );
$ Y1 = rand (0, 20 );
$ Y2 = rand (0, 20 );
$ Y3 = rand (0, 20 );
$ Y4 = rand (0, 20 );
Imageline ($ im, 0, $ y1, 60, $ y3, IMG_COLOR_STYLED );
Imageline ($ im, 0, $ y2, 60, $ y4, IMG_COLOR_STYLED );

// Randomly generate a large number of black spots on the canvas, which can interfere with the canvas;
For ($ I = 0; $ I <80; $ I ++)
{
Imagesetpixel ($ im, rand (0, 60), rand (0, 20), $ black );
}
// Display the four numbers randomly on the canvas. the horizontal spacing and position of the characters are randomly generated according to a certain fluctuation range.
$ Strx = rand (3, 8 );
For ($ I = 0; $ I <4; $ I ++ ){
$ Strpos = rand (1, 6 );
Imagestring ($ im, 5, $ strx, $ strpos, substr ($ num, $ I, 1), $ black );
$ Strx + = rand (8, 12 );
}
ImagePNG ($ im );
ImageDestroy ($ im );
?>



Usage:
This program can be run directly. after running the program, you can see a graphic verification code. every time you refresh the code, a new code is randomly generated.
When using this program on a page, you can use the following code:

The code is as follows:
.....
Enter the verification code:


.....


In this way, the verification code image is displayed. On the verification page, use the following code:

The code is as follows:
...
$ Code = $ _ POST ["passcode"];
If ($ code = $ _ SESSION ["Checknum"]) {
Verification passed
} Else {
Incorrect verification code
}
...


Running result

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.