PHP Verification Code Class

Source: Internet
Author: User

<?php/** * Created by Phpstorm. * User:administrator * DATE:2016/6/20 * time:14:29 */class captcha{//Verification code class protected $str = "XAAXQWE556232ASSD"  ; Random number protected $code; Verification Code protected $length = 4;  Verification code Length protected $width = 80; Verification code Width Protected $height = 30;        Verification code height protected $img;//verification code generation//Random number Public Function GetCode () {$len =strlen ($this->str)-1;        for ($i =0; $i <4; $i + +) {$this->code.= $this->str[mt_rand (0, $len)]; }}//Generate background Public function CODEBG () {//Create a new image $this->img=imagecreatetruecolor ($this->width        , $this->height);  $color =imagecolorallocate ($this->img,rand (1,100), rand (1,100), rand (1,100));        Red, green, blue//$back =imagecolorallocate ($this->img,0,0,0);    Imagefilledrectangle ($this->img,0,0, $this->width, $this->height, $color);         }//Generate interference element public function Setline () {//Interference point for ($i =0; $i <1000; $i + +) {   $color =imagecolorallocate ($this->img,rand (0,255), Rand (0,255), Rand (0,255));        Imagesetpixel ($this->img,rand (1,99), Rand (1,99), $color); }//////Interference line for ($i =0; $i <5; $i + +) {$color =imagecolorallocate ($this->img,rand (0,255), R            and (0,255), Rand (0,255)); Imageline ($this->img,rand (0, $this->width), rand (0, $this->height), rand (0, $this->width), rand (0, $this-        >height), $color);    }//////Interference line}//Generate element Header public Function Outheader () {Header ("content-type:image/png");        }//write Verification code Public Function WriteString () {$red = Imagecolorallocate ($this->img, 255, 0, 0);        Imagestring ($this->img,5,rand (1,15), Rand (1,15), $this->code (), $red);        $font = ' Arial.ttf ';    Imagettftext ($this->img, 0, ten, $red, $font, $this->code);        }//shu Public Function png () {$this->outheader (); $this-&GT;CODEBG ();        $this->getcode ();        $this->setline ();        $this->writestring ();        Session_Start ();        $_session[' code ']= $this->code ();    Imagepng ($this->img);    Public Function code () {return strtoupper ($this->code); }} $ce =new captcha (); $ce->png (); Var_dump ($_session[' code ');

Test page

<! DOCTYPE html>

  

PHP Verification Code Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.