Home  >  Article  >  Backend Development  >  A simple PHP verification code implementation code_PHP tutorial

A simple PHP verification code implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:01795browse

Implementation code:

//Save the verification code in the session for global use
session_start();
$nums = "";
for($i=0;$i<4;$i++){
//Generate random numbers and convert to hexadecimal
$nums.=dechex(mt_rand(0,15));
}
//Write the verification code into session
$_SESSION['code']=$nums;

//Set the verification code length and width
$_width = 60;
$_height = 20;
//Create an image
$_img = imagecreatetruecolor($_width,$_height);
//Create a white color
$_white = imagecolorallocate($_img ,220,250,250);
//Fill the background
imagefill($_img,0,0,$_white);

//Draw 6 lines randomly
for($i=0; $i<6;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width ),mt_rand(0,$_width),mt_rand(0,$_width),mt_rand(0,$_width),$_rnd_color);
}

//Draw snowflakes randomly
for ($i=0;$i<60;$i++){
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",imagecolorallocate($_img, mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}

//Output verification code
for($i=0;$i imagestring($_img,mt_rand(6,10),$i*$_width/4+mt_rand(1,10),mt_rand(1,$_height/2), $_SESSION['code'][$i],imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}

//Output and destruction
header("Content-Type:image/png");
imagepng($_img);
imagedestroy($_img);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768133.htmlTechArticleImplementation code: ?php //Save the verification code into the session for global use session_start(); $nums = ""; for($i=0;$i4;$i++){ //Generate random numbers and convert to hexadecimal $nums.=dechex(mt...
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