PhpGD database verification code generation instance
- Verification code
- Enter verification code
2. Generate the verification code auth. php
-
Session_start ();
- Header ("Content-type: image/png ");
-
$ Img_width = 100;
- $ Img_height = 20;
-
Srand (microtime () * 100000 );
- For ($ I = 0; $ I <4; $ I ++)
- {
- $ New_number. = dechex (rand (0, 15 ));
- }
-
$ _ SESSION [check_auth] = $ new_number;
- $ New_number = imageCreate ($ img_width, $ img_height); // create an image
- ImageColorAllocate ($ new_number, 255,255,255); // set the background color to white.
-
For ($ I = 0; $ I
- {
-
- $ Font = mt_rand (3, 5 );
- $ X = mt_rand (1, 8) + $ img_width * $ I/4;
- $ Y = mt_rand (1, $ img_height/4 );
- $ Color = imageColorAllocate ($ new_number, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200); // set the character color
- ImageString ($ new_number, $ font, $ x, $ y, $ _ SESSION [check_auth] [$ I], $ color); // output character
- }
-
ImagePng ($ new_number );
- ImageDestroy ($ new_number );
- ?>
3. submit the check_auth.php page
-
Session_start ();
- $ Auth = $ _ POST ['auth'];
-
If (empty ($ auth ))
- {
- Echo 'Error: the verification code cannot be blank ';
- Die;
- }
-
If ($ auth = $ _ SESSION ['Check _ auth'])
- {
- Echo 'true ';
- }
- Else
- {
- Echo 'Error: incorrect verification code input ';
- }
- ?>
|