PHP Verification Code, PHP captcha code
Design a verification code class that can be called at any time when needed
Verification code class, Save as ValidateCode.class.php
1
Php2 //Verification Code Class3 Session_Start();4 classValidatecode {5 Private $charset= ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 ';//Random Factor6 Private $code;//Verification Code7 Private $codelen= 4;//Verification Code Length8 Private $width= 130;//width9 Private $height= 50;//HeightTen Private $img;//Graphics Resource Handle One Private $font;//the specified font A Private $fontsize= 20;//Specify font size - Private $fontcolor;//Specify font Color - //Construction method initialization the Public function__construct () { - $this->font = './latha.ttf ';//Note that the font path must be written, otherwise it will not show the picture - } - //Generate random Code + Private functionCreatecode () { - $_len=strlen($this->charset)-1; + for($i= 0;$i<$this->codelen;$i++) { A $this->code. =$this->charset[Mt_rand(0,$_len)]; at } - } - //Generate Background - Private functionCreatebg () { - $this->img = Imagecreatetruecolor ($this->width,$this-height); - $color= Imagecolorallocate ($this->img,Mt_rand(157,255),Mt_rand(157,255),Mt_rand(157,255)); inImagefilledrectangle ($this->img,0,$this->height,$this->width,0,$color); - } to //Generate text + Private functionCreateFont () { - $_x=$this->width/$this-Codelen; the 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]);Panax Notoginseng } - } the //generate lines, snowflakes + Private functionCreateline () { A //Lines the 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); $ } $ //Snowflakes - for($i= 0;$i<100;$i++) { - $color= Imagecolorallocate ($this->img,Mt_rand(200,255),Mt_rand(200,255),Mt_rand(200,255)); theImagestring ($this->img,Mt_rand(1,5),Mt_rand(0,$this->width),Mt_rand(0,$this->height), ' * ',$color); - }Wuyi } the //Output - Private functionOutPut () { Wu Header(' Content-type:image/png '); -Imagepng ($this-img); AboutImagedestroy ($this-img); $ } - //External Generation - Public functiondoimg () { - $this-CREATEBG (); A $this-Createcode (); + $this-createline (); the $this-CreateFont (); - $this-OutPut (); $ } the //Get Verification Code the Public functionGetCode () { the return Strtolower($this-code); the } -}
Note: In line 16th, to modify the path of the font, the font picture cannot be displayed
Implemented, saved as captcha.php
1 Session_Start (); 2 require './validatecode.class.php '; // first, the class is included and the actual path is modified according to the actual situation. 3$_vcnew validatecode (); // instantiate an object 4 $_VC, doimg (); 5 $_session $_VC->getcode (); // The verification code is saved to the session
Page usage
< img title = "Click to refresh" src = "./captcha.php" Align = "Absbottom" onclick = "this.src= ' captcha.php?" +math.random (); "
>
img >
Reprinted from: A Beautiful PHP Verification code class (share)
http://www.bkjia.com/PHPjc/973822.html www.bkjia.com true http://www.bkjia.com/PHPjc/973822.html techarticle PHP Verification Code, PHP captcha code design a verification code class, when necessary, you can call the code class at any time, save as ValidateCode.class.php 1? PHP 2//Verification code Class 3 session ...