It is very convenient to implement verification code in PHP, the key point is to master the use of PHP GD Library and session.
Throughout the online PHP generation Verification code example, is not all PHP GD library and session combination, and the use of PHP generated random number method to complete.
PHP Verification code, can be divided into many kinds, including PHP image verification Code, PHP random verification Code, as well as PHP Chinese verification code, according to different applications to use different verification code.
Here to share a PHP digital verification code, for everyone's 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 "verified success! "; else echo "Validation failed! "; } Generate a new four-bit integer verification Code //while (($authnum =rand ()%10000) <1000); ? > <formaction=test4.phpmethod=post> <table> Please enter your verification code: <inputtype=textname= Authinputstyle= "width:80px" ><br> <inputtype=submitname= "Verify" value= "Submit verification Code" > < Inputtype=hiddenname=authnumvalue=<?echo$authnum;? >> > </table> </form>
authimg.php
<?php //Generate Verification code picture Header ("Content-type:image/png"); Srand (Double) microtime () *1000000);//sow a seed that generates random numbers to facilitate the use of session_start () generated by the random number below ;//Put a random number in the session $_ session[' Authnum ']= ""; $im =imagecreate (62,20);//Make picture background size $black =imagecolorallocate ($im, 0,0,0);//Set three colors $white = Imagecolorallocate ($im, 255,255,255); $gray =imagecolorallocate ($im, 200,200,200); Imagefill ($im, 0,0, $gray);//using the area Fill method, set (0,0) while (($authnum =rand ()%100000) <10000); Draws a four-bit integer verification code into the picture //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, y coordinates of the image represented by image (the upper-left corner of the image is 0,0). //If the font is 1,2,3,4 or 5, use the built-in font for ($i =0; $i <200; $i + +)//Add interference pixel { $randcolor = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255)); Imagesetpixel ($im, Rand ()%70,rand ()%30, $randcolor); } Imagepng ($im); Imagedestroy ($im);
PHP Digital Verification Code