PHP implementation of simple and practical Verification code class, PHP simple and practical Verification code
This paper introduces the simple and practical verification code class of PHP implementation. Share to everyone for your reference. Specific as follows:
<?php/*** @version 1.0* @author bolted snail* @date 2011-10-15* @PHP Verification Code class * Method of Use: * $image =new Captcha (); * $image->con Fig (' width ', ' height ', ' number of characters ', ' Verification Code Session index '); * $image->create ();//This will output a picture to the browser *//All parameters can be omitted, * default is: Width 80 high 20 characters 4 Verification Code SESSION Index captcha_code* The fourth parameter is to save the verification code to $_session[' Captcha_code ']* The simplest example of use: * $image =new captcha (); * $image->create ();//This will output a picture to the browser */class captcha{private $width =80, $height =20, $codenum =4;public $checkcode; The generated verification code private $checkimage; Verification code Picture Private $disturbColor = '; Interference pixel private $session _flag= ' captcha_code ';//The index in the session//attempt to start sessionfunction __construct () {@session_start ();} /** parameters: (width, height, number of characters) */function config ($width = ' ", $height = '", $codenum = ' 4 ', $session _flag= ' Captcha_code ') {$this- >width= $width; $this->height= $height; $this->codenum= $codenum; $this->session_flag= $session _flag;} function Create () {//Output header $this->outfileheader (); Generate Verification Code $this->createcode (); Produce picture $this->createimage (); Set the interference pixel $this->setdisturBcolor (); Write the verification code on the picture $this->writecheckcodetoimage (); Imagepng ($this->checkimage); Imagedestroy ($this->checkimage); $_session[$this->session_flag]= $this->checkcode;} /* * @brief Output header */private function Outfileheader () {Header ("Content-type:image/png");} /** * Generate Verification code */private function Createcode () {$this->checkcode = Strtoupper (substr (MD5 (rand ()), 0, $this->codenum)) ;} /** * Generate Verification code picture */private function CreateImage () {$this->checkimage = @imagecreate ($this->width, $this->height); $back = Imagecolorallocate ($this->checkimage,255,255,255); $border = Imagecolorallocate ($this->checkimage,0,0,0); Imagefilledrectangle ($this->checkimage,0,0, $this->width-1, $this->height-1, $back); White Bottom Imagerectangle ($this->checkimage,0,0, $this->width-1, $this->height-1, $border); Black Border}/** * Set image interference pixels */private function Setdisturbcolor () {for ($i =0; $i <=200; $i + +) {$this->disturbcolor = Imagecolorallocate ($this->checkimagE, rand (0,255), Rand (0,255), Rand (0,255)); Imagesetpixel ($this->checkimage,rand (2,128), Rand (2,38), $this->disturbcolor); }}/** * * */private function Writecheckcodetoimage () {for ($i =0; $i < $this->codenum; $i + +) on the verification code picture. {$b G_color = Imagecolorallocate ($this->checkimage, rand (0,255), Rand (0,128), Rand (0,255)); $x = Floor ($this->width/$this->codenum) * $i; $y = rand (0, $this->height-15); Imagechar ($this->checkimage, rand (5,8), $x +5, $y, $this->checkcode[$i], $bg _color); }}function __destruct () {unset ($this->width, $this->height, $this->codenum, $this->session_flag);}}? >
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1039207.html www.bkjia.com true http://www.bkjia.com/PHPjc/1039207.html techarticle PHP Implementation of simple and practical Verification code class, PHP simple and practical Verification code in this paper, the implementation of PHP simple and practical verification code class. Share to everyone for your reference. Specific as follows: php/*** ...