Home > Backend Development > PHP Tutorial > PHP dynamically randomly generates verification code code_PHP tutorial

PHP dynamically randomly generates verification code code_PHP tutorial

WBOY
Release: 2016-07-21 15:39:27
Original
938 people have browsed it

The following is the rendering. This rendering is the rendering without interference code enabled
PHP dynamically randomly generates verification code code_PHP tutorial
The following is the class code

Copy code The code is as follows:

/************************************************
/ /FILE:ImageCode
//DONE: Generate dynamic verification code class
//DATE"2010-3-31
//Author:www.5dkx.com 5D Happy Blog
**** *************************************************** ********************/
class ImageCode{
private $width; //Verification code image width
private $height; //Height of verification code image
private $codeNum; //Number of verification code characters
private $checkCode; //Verification code characters
private $image; //Verification code canvas
/***************************************************** ************************
// Function: Constructor
// Done: Member attribute initialization
// Author: www.5dkx. com 5D Happy Blog
************************************************ *****************************/
function __construct($width=60,$height=20,$codeNum=4)
{
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->checkCode = $this->createCheckCode();
}
function showImage()
{
$this->getcreateImage();
$this->outputText();
$this->setDisturbColor();
$this- >outputImage();
}
function getCheckCode()
{
return $this->chekCode;
}
private function getCreateImage()
{
$this->image = imagecreatetruecolor($this->width,$this->height);
$back = imagecolorallocate($this->image,255,255,255);
$border = imagecolorallocate ($this->image,255,255,255);
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
//Use pure white to fill the rectangular frame. If used here, the subsequent interference code will be invalid
/*If you want to use interference code, use the following*/
//imagerectangle($this->image,0, 0,$this->width-1,$this->height-1,$border);
}
private function createCheckCode()
{
for($i=0; $i<$this->codeNum;$i++)
{
$number = rand(0,2);
switch($number)
{
case 0: $rand_number = rand(48,57); break;//Numbers
case 1: $rand_number = rand(65,90);break;//Capital letters
case 2: $rand_number = rand(97,122);break ;//Lowercase letters
}
$asc = sprintf("%c",$rand_number);
$asc_number = $asc_number.$asc;
}
return $asc_number;
}
private function setDisturbColor()//Interference setting
{
for($i=0;$i<=100;$i++)
{
//$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
$color = imagecolorallocate($this->image,255,255,255);
imagesetpixel($ this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
//$color = imagecolorallocate($this->image,0,0,0);
//imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this ->height-2),$color);
}
private function outputText()
{
//Random color, random placement, random string output to image
for ($i=0;$i<=$this->codeNum;$i++)
{
$bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand (0,255));
$x = floor($this->width/$this->codeNum)*$i+3;
$y = rand(0,$this->height- 15);
imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color);
}
}
private function outputImage()
{
if(imagetypes()&IMG_GIF)
{
header("Content_type:image/gif");
imagegif($this->image);
}
elseif(imagetypes()&IMG_JPG)
{
header("Content-type:image/jpeg");
imagejpeg($this->image,"",0.5) ;
}
elseif(imagetypes()&IMG_PNG)
{
header("Content-type:image/png");
imagejpeg($this->image);
}
elseif(imagetypes()&IMG_WBMP)
{
header("Content-type:image/vnd.wap.wbmp");
imagejpeg($this->image);
}
else
{
die("PHP does not support image creation");
}
}
function __destruct()
{
imagedestroy($ this->image);
}
}
/*display*/
/*******************************************************************
session_start();
$image = new ImageCode(60,20,4);
$image->showImage();
$_SESSION['ImageCode'] = $image->getCheckCode();
*******************************************************************/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321592.htmlTechArticleThe following is the rendering. This rendering is the rendering without interference code. The following is the class code. Copy the code. The code is as follows : ?php /********************************************** ** //FILE:...
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