In PHP to implement the verification code is very convenient, the key point is to master PHP library and the use of session.
Throughout the online PHP generated verification code examples, are all PHP GD Library and session of the combination, and using PHP to generate random number of methods to complete.
PHP Verification code, can be divided into a number of types, including PHP image Verification Code, PHP random Verification Code, as well as the PHP Chinese Verification Code , according to different applications to use different verification code.
Here to share a PHP digital verification code, for your reference.
4-bit digital authentication code
/* *filename:authpage.php
* *
session_start ();
Srand (Double) microtime () *1000000);
$authnum =$_session[' Authnum '];
Verify that user input is consistent with the Authenticode if
(Isset ($_post[' authinput '))
{
if (strcmp $_post[' authinput '],$_session[') Authnum '] ==0)
echo "Validation successful! ";
else
echo "Validation failed! ";
}
Generates a new four-bit integer authentication Code
//while (($authnum =rand ()%10000) <1000);
? >
<formaction=test4.phpmethod=post>
<table>
Please enter the verification code: <inputtype=textname= Authinputstyle= "width:80px" ><br>
<inputtype=submitname= "Validation" value= "Submit verification Code" >
< Inputtype=hiddenname=authnumvalue=<?echo$authnum;? >>
>
</table>
authimg.php
<?php
//Generate Verification code picture
Header ("Content-type:image/png");
Srand ((double) Microtime () *1000000);//sow a seed that generates a random number to facilitate the use of the following random number generation
session_start ();//Put the random number into session
$_ session[' authnum ']= ' ";
$im =imagecreate (62,20)//Setting the 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 region filling method, set (0,0) while
($authnum =rand ()%100000) <10000);
Draw a four-bit integer verification code into the picture
$_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 images (the upper-left corner of the image is 0,0).
//If 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);
? >
The above is the PHP to generate 4-bit digital verification code to achieve codes, I hope that the learning of everyone help, we can more skillfully grasp the operation of PHP verification code.