Home  >  Article  >  Backend Development  >  A simple PHP graphic verification code generation program_PHP tutorial

A simple PHP graphic verification code generation program_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:42981browse

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.

/**
 *
 * @file imgvcode.php
 * @create date 2007-09-25
 * @copyright (c) 2005 - 2007 eifr.com
 * @license http://www.hzhuti.com/nokia/n97/
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

 * eifr is free software
 */

session_start();

$_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...
Statement:
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