PHP Implementation Verification Code function

Source: Internet
Author: User
Tags border color php server set background

Verification code Such a function can be said to be everywhere, and then use PHP to implement the verification code such functions, here I was to extract the verification code implementation of a class independent, then if you use the code function, directly introduced the class file and create an instance of the class, you can use the verification code, the code is as follows:

Verification code class file vcode.class.php

<?php/** * Verification Code class */class Vcode{private $width;//Picture width private $height;//Picture height private $num;//The number of authentication code is private $img;//Picture Resource pri Vate $code;//authentication code private $pointNum;//The number of interference points private $lineNum;//number of interference lines private $fontFile;//font file//constructor initialize related data function __ Construct ($width =85, $height =34, $num =4) {$this->width= $width; $this->height= $height; $this->num= $num; $ This->code= $this->createcode (), $this->pointnum=100; $this->linenum=10; $this->fontfile= "STLITI. TTF ";} /** * Used to set member properties * @param string $key member property name * @param mixed $value member property value * @return object returns its own $this, which can be used for coherent operation */public Funct Ion Set ($key, $val) {//get_class_vars () gets the array of properties in the class//get_class () returns the class name of the object if (Array_key_exists ($key, Get_class_vars ( Get_class ($this))) {$this->setoption ($key, $val);} return $this;} Set parameters Private Function SetOption ($key, $value) {$this, $key = $value;} Get the Captcha public Function GetCode () {return $this->code;} Output image Public Function outimg () {//Create image $this->createimage ();//Draw Verification Code $this->drawcode ();//Draw interfering element $this-> Drawdisturbcolor();//Output Image $this->printimg ();} Draw Verification Code Private Function Drawcode () {$this->fontfile= "./font/". $this->fontfile;for ($i =0; $i < $this->num;$ i++) {//Set random color $randcolor=imagecolorallocate ($this->img,rand (0,128), Rand (0,128), Rand (0,128));//font size $fontsize= Rand (20,23);//font horizontal position $x= ($this->width/$this->num) * $i;//Horizontal position $y=rand ($fontSize, Imagefontheight ($fontSize ) +3)///Draw font Imagettftext ($this->img, $fontSize, 0, $x, $y, $randColor, $this->fontfile, $this->code{$i});}} Draw interference elements Private function drawdisturbcolor () {//Draw interference points for ($i =0; $i < $this->pointnum; $i + +) {//Set random color $randcolor= Imagecolorallocate ($this->img,rand (0,255), Rand (0,255), Rand (0,255));//Draw Point Imagesetpixel ($this->img,rand (1,$ THIS-&GT;WIDTH-2), rand (1, $this->height-2), $randColor);} Draw interference lines for ($i =0; $i < $this->linenum; $i + +) {//Set random color $randcolor=imagecolorallocate ($this->img,rand (0,200), Rand (0,200), Rand (0,200));//Draw line Imageline ($this->img,rand (1, $this->width-2), rand (1, $this->height-2), Rand (1, $this->height-2), rand (1, $this->width-2), $randColor);}} Create Image Private Function createimage () {//Create a true Color image $this->img=imagecreatetruecolor ($this->width, $this height);//Set Background color $bgcolor=imagecolorallocate ($this->img,rand (200,255), Rand (200,255), Rand (200,255));// Fill background color Imagefill ($this->img,0,0, $bgColor);//Set Border color $bordercolor=imagecolorallocate ($this->img,0,0,0);// Draw a border Imagerectangle ($this->img,0,0, $this->width-1, $this->height-1, $borderColor);} Output image Private Function printimg () {if (Imagetypes () & Img_png) {//For Pngheader ("Content-type:image/png"); Imagepng ( $this->img);} else if (Imagetypes () & img_jpg) {//For Jpgheader ("Content-type:image/jpeg"); imagejpeg ($this->img,null,100);} else if (Imagetypes () & Img_gif) {//For Gifheader ("Content-type:image/gif"); Imagegif ($this->img);} else if (Imagetypes () & img_wbmp) {//For Wbmpheader (' content-type:image/vnd.wap.wbmp '); Imagewbmp ($this->img);} Else{die (' No image ' in this PHP server ');}} Create Verification Code Private Function Createcode () {//default string $codes= "123456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY ";//Generate Verification Code $code=" "for ($i =0; $i < $this->num; $i + +) {$ code.= $codes {rand (0,strlen ($codes)-1};} return $code;}  Destructors are used to destroy image resources function __destruct () {Imagedestroy ($this->img);} }

Here I use the font of the function is Imagettftext (), because this function can customize the font style, from the code can also be seen, passed parameters have a font file attributes, if you do not like using this function can use the imagestring () function also line, It's just that the default font size for this function doesn't look good. Or customize the font to look more comfortable. The next step is to invoke the Captcha class

<?php//open Sessionsession_start ();//Introduce the Code class include ("vcode.class.php");//Create a Verification code class $vcode=new Vcode ();// Save the obtained verification code to the SESSION $_session[' code ']= $vcode->getcode ();//$vcode->set ("Pointnum", 10);//custom number of interference points//$vcode- >set ("LineNum", 10);//custom number of interference lines//$vcode->set ("Fontfile", "Wawa.ttf");//Custom font file//output image $vcode->outimg ();

The code is implemented here, and a direct call to the file will see the verification code, using a simple login form

<?php//open Sessionsession_start ();//Determine whether to commit if (Isset ($_post[' Dosubmit ')) {//Get the verification code in session and turn to lowercase $sessioncode= Strtolower ($_session[' Code '));//Get Input Verification code $code=strtolower ($_post[' code ');//Determine equality if ($sessionCode = = $code) {echo] <script type= ' text/javascript ' >alert (' captcha correct! '); </script> ";} Else{echo "<script type= ' text/javascript ' >alert (' captcha error! '); </script> ";}}? ><! DOCTYPE html>
The effect of the implementation:


PHP Implementation Verification Code function

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.