Home > Article > Backend Development > Verification code generation program and usage_PHP tutorial
Verification code generation program and usage method The following is a verification code generation program using PHP. It uses the srand function to generate the verification image and uses the image function of the PHP gd library to generate the verification image, and then saves the generated data to the session global variable.
Verification code generation program and usage
The following is a verification code generation program using PHP tutorial. It uses srand random function to generate verification images and uses the image function of PHP gd library to generate verification images, and then generates The data is saved to session global variables.
//Calling this page, if the following formula is true, generate a verification code image
if($_get['action']=='verifycode'){
rand_create();
}
//Verification code image generation
function rand_create(){
//Notify the browser that a png image will be outputheader('content- type: image/png');
//Prepare the random number generator seed
srand((double)microtime()*1000000);
//Prepare the relevant parameters of the image
$im = imagecreate(62,20);
$black = imagecolorallocate($im, 0,0,0); //rgb black identifier
$white = imagecolorallocate($im, 255,255,255); / /rgb white identifier
$gray = imagecolorallocate($im, 200,200,200); //rgb gray identifier
//Start drawing
imagefill($im,0,0,$gray);
while(($randval=rand()%100000)<10000);{
//Draw the four-digit integer verification code into the picture
session_start();
$_session['login_check_num' ] = $randval;
imagestring($im, 5, 10, 3, $randval, $black);
}
//Add interference pixels
for($i=0;$ 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);
}
?>
Call method
Determine whether the verification code is entered correctly
if( $_post['code'] == $_session['login_check_num'] )
{
echo ' Verification code is correct';
}
else
{
echo 'Verification code is incorrect';
}