It is very convenient to implement the verification code in php. The key point is to master the usage of the php gd library and session.
Looking at the online php verification code generation example, there is no combination of php gd library and session, and php is used to generate a random number.
PHP verification codes can be divided into many types, including php image verification codes, php Random verification codes, and php Chinese verification codes. Different verification codes are used for different application scenarios.
Here is a php digital verification code for your reference.
1. Digital Verification Code
- /*
- * Filename: authpage. php
- */
- Session_start ();
- // Srand (double) microtime () * 1000000 );
- $ Authnum = $ _ SESSION ['authnum'];
- // Verify that the user input is consistent with the verification code
- If (isset ($ _ POST ['authinput'])
- {
- If (strcmp ($ _ POST ['authinput'], $ _ SESSION ['authnum']) = 0)
- Echo "Verification Successful! ";
- Else
- Echo "Verification Failed! ";
- }
- // Generate a new four-digit integer Verification Code
- // While ($ authnum = rand () % 10000) <1000 );
- ?>
- <Formaction = test4.phpmethod = post>
- <Table>
- Enter the verification code: <inputtype = textname = authinputstyle = "width: 80px"> <br>
- <Inputtype = submitname = "verify" value = "Submit verification code">
- <Inputtype = hiddenname = authnumvalue = <? Echo $ authnum;?>
- </Table>
- </Form>
2, authimg. php
- <? Php
- // Generate a verification code Image
- Header ("Content-type: image/PNG ");
- Srand (double) microtime () * 1000000); // play the next seed that generates random numbers to facilitate the use of the following Random Number Generation
- Session_start (); // Save the random number to the session.
- $ _ SESSION ['authnum'] = "";
- $ Im = imagecreate (); // specify the image background size.
- $ Black = ImageColorAllocate ($ im, 0, 0); // you can specify three colors.
- $ White = ImageColorAllocate ($ im, 255,255,255 );
- $ Gray = ImageColorAllocate ($ im, 200,200,200 );
- Imagefill ($ im, $ gray); // use the area filling method, set)
- While ($ authnum = rand () % 100000) <10000 );
- // Print the four-digit integer verification code into the image
- // Www.jbxue.com
- $ _ SESSION ['authnum'] = $ authnum;
- Imagestring ($ im, 5, 10, 3, $ authnum, $ black );
- // Use the col color to draw the string s to the x and y coordinates of the image (0, 0 in the upper left corner of the image ).
- // If the font is 1, 2, 3, 4, or 5, use the built-in font.
- For ($ I = 0; $ I <200; $ I ++) // Add interference pixels
- {
- $ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
- Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
- }
- ImagePNG ($ im );
- ImageDestroy ($ im );
- ?>