Verification code generation and application instance
On the write user verification page, such as registration and logon, add verification codes to enhance the security of user logon.
The verification code uses gd to generate a png image, and assigns $ randval random numbers to $ _ session ['login _ check_num ']. The verification code is compared with $ _ post, to determine whether it is correct. To achieve the required functions, You need to modify the php tutorial. ini file so that php supports the gd library.
<? Php
// Call this page. If the following formula is set up, a verification code image is generated.
If ($ _ get ["action"] = "verifycode ")
{
Rand_create ();
}
// Verification Code Image Generation
Function rand_create ()
{
// Notify the browser that a png image will be output
Header ("content-type: image/png ");
// Prepare the random number generator Seeds
Srand (double) microtime () * 1000000 );
// Prepare Image Parameters
$ Im = imagecreate (62,20 );
$ Black = imagecolorallocate ($ im, 0); // rgb black identifier
$ White = imagecolorallocate ($ im, 255,255,255); // rgb white identifier
$ Gray = imagecolorallocate ($ im, 200,200,200); // rgb gray identifier
// Start plotting
Imagefill ($ im, 0, 0, $ gray );
While ($ randval = rand () % 100000) <10000 );{
$ _ Session ["login_check_num"] = $ randval;
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 3, $ randval, $ black );
}
// Add interference pixels
For ($ I = 0; I I <200; $ I ++ ){
$ Randcolor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
}
// Output verification Image
Imagepng ($ im );
// Destroy the image identifier
Imagedestroy ($ im );
}
// Verify the verification code
Function rand_check ()
{
If ($ _ post ["reg_rand"] = $ _ session ["login_check_num"]) {
Return true;
}
Else {
Exit ("Incorrect verification code ");
}
}
?>
A verification code is a public, fully automated program that distinguishes users from computers and people. In the captcha test, the server computer will automatically generate a question to be answered by the user. 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.
*/
Session_start ();
$ String = null;
$ Im = imagecreatetruecolor (60, 25); // create a true color chart 60*25
$ Bg = imagecolorallocate ($ im, 255,255,255); // white background
Imagefill ($ im, $ bg); White filled
$ X = 5 ;//
$ Y = 0; // text Coordinate
For ($ I = 0; $ I & lt; 4; $ I ++)
{
$ Char = mt_rand (0, 9 );
$ String. = $ char;
$ Y = mt_rand (0, 10 );
$ Ccolor = imagecolorallocate ($ im, mt_rand (0,230), mt_rand (0,230), mt_rand (0,230 ));
Imagechar ($ im, 6, $ x, $ y, $ char, $ ccolor); // fill in text
$ X + = mt_rand (10, 15 );
}
For ($ I = 0; $ I
{
$ X1 = mt_rand (0, 80 );
$ X2 = mt_rand (0, 80 );
$ Y1 = mt_rand (0, 30 );
$ Y2 = mt_rand (0, 30 );
$ X2 = $ x1 + mt_rand (1, 5 );
$ Y2 = $ y1 + mt_rand (1, 5 );
$ Lc = imagecolorallocate ($ im, mt_rand (0,230), mt_rand (0,230), mt_rand (0,230 ));
Imageline ($ im, $ x1, $ y1, $ x2, $ y2, $ lc); // fill the line
}
$ _ Session ['code'] = md5 ($ string );
Header ("content-type: image/jpeg ");
Imagepng ($ im );
Imagedestroy ($ im );
For more details, see http://www.bKjia. c0m/phper/php-cy/33707.htm
Of course, we can also know that php ajax can be used for instance verification.