The principle of generating verification code is quite simple. It is to use mt_rand to randomly generate a number, and then save it in the session to judge whether the entered verification code is consistent with the one we generated when the user logs in. Then the random number is generated using the php gd function. Picture, this completes the generation of the verification code.
The code is as follows
代码如下 |
复制代码 |
/**
*
* @file imgvcode.php
* @create date 2007-09-25
* @copyright (c) 2005 - 2007 eifr.com
* @license http://www.hzhuti.com/nokia/n97/
* eifr is free software
*/
session_start();
// main
$vcodes = '';
//generate Number 4
srand((double)microtime()*1000000);
for($i=0;$i<4;$i++){
$vcodes.=rand(1,9);
}
$_SESSION['eifr_checkvcode'] = $vcodes;
if(function_exists('imagecreate')){
//generate picture validation code
Header("Content-type: image/PNG");
$img = imagecreate(44,18);
$bg = ImageColorAllocate($img, 245,245,245);
imagefill($img,0,0,$bg); //background
//generate Number 4
for($i=0;$i<4;$i++){
$font = ImageColorAllocate($img, rand(100,255),rand(0,100),rand(100,255));
$vnum = substr($vcodes, $i, 1);
imagestring($img, 5, 2+$i*10, 1, $vnum, $font);
}
//add interference
for($i=0;$i<100;$i++)
{
$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($img);
ImageDestroy($img);
}
?>
|
|
Copy code
|
|
/**
*
* @file imgvcode.php
* @create date 2007-09-25
* @copyright (c) 2005 - 2007 eifr.com
* @license http://www.hzhuti.com/nokia/n97/
* eifr is free software
*/
session_start();
// main
$vcodes = '';
//generate Number 4
srand((double)microtime()*1000000);
for($i=0;$i<4;$i++){
$vcodes.=rand(1,9);
}
$_SESSION['eifr_checkvcode'] = $vcodes;
if(function_exists('imagecreate')){
//generate picture validation code
Header("Content-type: image/PNG");
$img = imagecreate(44,18);
$bg = ImageColorAllocate($img, 245,245,245);
Imagefill($img,0,0,$bg); //background
//generate Number 4
for($i=0;$i<4;$i++){
$font = ImageColorAllocate($img, rand(100,255),rand(0,100),rand(100,255));
$vnum = substr($vcodes, $i, 1);
Imagestring($img, 5, 2+$i*10, 1, $vnum, $font);
}
//add interference
for($i=0;$i<100;$i++)
{
$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img, rand()%70, rand()%30, $randcolor);
}
ImagePNG($img);
ImageDestroy($img);
}
?>
Note: PHP needs to open the PHP gd image library to generate the verification code, otherwise it cannot be generated.
http://www.bkjia.com/PHPjc/631290.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631290.htmlTechArticleThe principle of generating verification code is quite simple. It is to use mt_rand to randomly generate a number, and then save it to the session for user login. Determine whether the entered verification code is consistent with the one we generated, then...