Directly upload the code:
Copy code The code is as follows:
//Verification code class
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//Random factor
private $code;//Verification code
private $codelen = 4;//Verification code length
private $width = 130;//Width
private $height = 50;//Height
private $img;//Graphic resource handle
private $font;//Specified font
private $fontsize = 20;//Specified font size
private $fontcolor;//Specify font color
//Constructor initialization
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.ttf ';//Please note that the font path must be written correctly, otherwise the picture cannot be displayed
}
//Generate random code
private function createCode() {
$_len = strlen($this->charset )-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0, $_len)];
}
}
//Generate background
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this ->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0 ,$this->height,$this->width,0,$color);
}
//Generate text
private function createFont() {
$_x = $this- >width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this- >img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x* $i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//Generate lines and snowflakes
private function createLine() {
//Line
for ($i=0;$i<6;$i++) {
$ color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width) ,mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
// Snowflake
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)) ;
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color );
}
}
//Output
private function outPut() {
header('Content-type:image/png');
imagepng($this-> ;img);
imagedestroy($this->img);
}
//Generate externally
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//Get verification code
public function getCode() {
return strtolower($this->code);
}
}
Output Example:
Usage:
1. First save the verification code class as a file named ValidateCode.class.php;
2. Create a new file named captcha.php file to call this class;
captcha.php
Copy the code The code is as follows:
session_start();
require './ValidateCode.class.php'; //Include the class first, and modify the actual path according to the actual situation.
$_vc = new ValidateCode(); //Instantiate an object
$_vc->doimg();
$_SESSION['authnum_session'] = $_vc->getCode();/ /Save the verification code in SESSION
3. Quote it into the page, the code is as follows:
Copy the code The code is as follows:
4. A complete verification page, the code is as follows:
Copy the code The code is as follows:
session_start();
//On the page, you must first open the session,
//error_reporting(2047);
session_destroy();
//Remove the session so that it can be retrieved every time New session value;
//Using seesion works well and is very convenient
?>
session image verification example< ;/title>
//Print the previous session ;
//echo "Previous session:
".$_SESSION["authnum_session"]."";
$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "What you just entered is: ".$_POST["validate"]."
Status: ";
if($validate!=$_SESSION["authnum_session"]){
//Determine whether the session value is consistent with the verification code entered by the user;
echo "< font color=red>Incorrect input";
}else{
echo "
Passed verification";
}
}
?>
Full demo download: demo
http://www.bkjia.com/PHPjc/328147.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328147.htmlTechArticleDirectly upload the code: Copy the code as follows: //Verification code class class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789' ;//Random factor private $code;...