Home > Article > Backend Development > php verification code_PHP tutorial
/**
* Verification code
* 2011/8/21
* kcj
**/
include "isLogin.php";
//Randomly generate a 4-digit verification code
$num='';
for($i=0;$i<4;$i++){
$num.=dechex(rand(0,20)); //The dechex function converts decimal to binary
}
session_start(); //Start session
$_SESSION['yanzheng']=$num; //Use session to remember this verification number
header("Content-type:image/PNG");
$im=imagecreate(60,20); //Create a canvas
$back=imagecolorallocate($im,rand(0,55),rand(0,20),rand(0,5));//Create a background color (black)
$gray=imagecolorallocate($im,rand(0,255),rand(0,200),rand(0,55)); //(white)
imagefill($im,0,0,$gray); //Fill color
$style=array($back,$back,$back,$back,$back,$gray,$gray,$gray,$gray,$gray); //Generate array
imagesetstyle($im,$style); //Set the line drawing style
$y1=rand(0,20);
$y2=rand(0,20);
$y3=rand(0,20);
$y4=rand(0,20);
imageline($im,0,$y1,60,$y3,IMG_COLOR_STYLED); //Draw a line
imageline($im,0,$y2,60,$y4,IMG_COLOR_STYLED);
// Randomly generate a large number of black dots on the canvas to interfere
for ($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,60),rand(0,20),$back);
}
$str=rand(3,8);
for ($i=0;$i<4;$i++){
$strp=rand(1,6);
Imagestring($im,6,$str,$strp,substr($num,$i,1),$back);
$str+=rand(8,12);
}
ImagePNG($im);
imagedestroy($im);
?>
Excerpted from chaojie2009’s column