Personal PHP verification Code generation class sharing, PHP verification code generation sharing
This verification code class can be used directly, you can also refer to!
Where class member Codestr is the generated Authenticode string:
<?php/** * Verification Code */class code{//1. Define each member has width, height, canvas, word number, type, draw type private $width;//width private $height;//Height private $num; Verification code words private $imgType; Generate picture type private $Type; String type three options 1 pure digit 2 Pure lowercase Letter 3 Case number mixed private $HB; Canvas public $codestr; Verify code string Public function __construct ($height =20, $num =4, $imgType = "jpeg", $Type =1) {$this->width = $num *20; $this->height = $height; $this->num = $num; $this->imgtype = $imgType; $this->type = $Type; $this->codestr = $this->codestr (); $this->zuhe (); }//2. Defines a random get String function Private Function codestr () {switch ($this->type) {Case 1://Type 1 gets 1-9 random number $str = Imp Lode ("", Array_rand (range (0,9), $this->num)); Break Case 2://Type 2 gets a-Z random lowercase letter $str = Implode ("", Array_rand (Array_flip (range (a,z)), $this->num)); Break Case 3://Type 3 get numbers, lowercase letters, uppercase letters mixed for ($i =0; $i < $this->num; $i + +) {$m = rand (0,2); Switch$m) {case 0: $o = rand (48,57); Break Case 1: $o = rand (65,90); Break Case 2: $o = rand (97,122); Break } $str. = sprintf ("%c", $o); } break; } return $STR; }//3. Initialize Canvas image resource Private Function Hb () {$this->HB = Imagecreatetruecolor ($this->width, $this->height); }//4. Generate background color private function Bg () {return imagecolorallocate ($this->hb,rand (130,250), Rand (130,250), Rand (130,250)); }//5. Generate font Color private function font () {return imagecolorallocate ($this->hb,rand (0,100), Rand (0,100), Rand (0,100)); }//6. Fill background color Private function BgColor () {imagefilledrectangle ($this->hb,0,0, $this->width, $this->height, $this- >BG ()); }//7. Disturbance Point Private Function Ganrao () {$sum =floor (($this->width) * ($this->height)/3); for ($i =0; $i < $sum; $i + +) {Imagesetpixel ($this->hb,rand (0, $this->width), rand (0, $this->height), $this->BG ()); }}//8. Random straight arc Private Function Huxian () {for ($i =0; $i < $this->num; $i + +) {Imagearc ($this->hb,rand (0, $this->wid TH), rand (0, $this->height), rand (0, $this->width), rand (0, $this->height), rand (0,360), Rand (0,360), $this- >BG ()); }}//9. Write Private Function Xiezi () {for ($i =0; $i < $this->num; $i + +) {$x =ceil ($this->width/$this->num) * $i; $y =rand (1, $this->height-15); Imagechar ($this->hb,5, $x +4, $y, $this->codestr[$i], $this->font ()); }}//10. Output Private Function outimg () {$shuchu = "image". $this->imgtype; $header = "content-type:image/". $this->imgtype; if (function_exists ($shuchu)) {header ($header); $shuchu ($this->HB); }else{exit ("GD library does not have such images"); }}//11. Assemble private Function Zuhe () {$this->HB (); $this->bgcolor (); $this->ganrao (); $this->huxian (); $this->xiezi (); $thiS->outimg (); } public Function Getcodestr () {return $this->codestr; }}?>
PHP generated verification code can not generate pictures, are the default image of the picture is marked with the Code GD library has
Share a verification code experience: in PHP generated verification code does not display properly, first of all you have to open this code in the browser of the PHP script to see, if a similar "picture error" of some information, your PHP program is not working properly, and then you put the "header (" Content-type:image/png "); "This sentence is removed, and then the page is refreshed. You can find the problem and solve it step-by-step with the error. Finally, the" header ... "words add, and then look again.
"Header (" Content-type:image/png ");" The meaning of this sentence is: Tell the browser "I" is a picture. With this phrase, the browser will not be normal to display PHP errors
The code that writes the random verification Code in PHP which has
Here are the PHP verification code classes.
Www.phpha.com/archives/31.html
http://www.bkjia.com/PHPjc/867252.html www.bkjia.com true http://www.bkjia.com/PHPjc/867252.html techarticle Personal write PHP verification code generation class sharing, PHP verification code generation share this code class directly take it can be used, can also refer to! Where class member Codestr is the generated Authenticode string ...