Home > Backend Development > PHP Tutorial > Verification code generation program and usage_PHP tutorial

Verification code generation program and usage_PHP tutorial

WBOY
Release: 2016-07-20 11:08:27
Original
791 people have browsed it

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 output

header('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';
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444893.htmlTechArticleVerification code generation program and how to use it. Here is a verification code generation program using php, using srand. Function generation and use the image function of the php gd library to generate verification images, and then generate...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template