PHP Verification Code Class Validatecode

Source: Internet
Author: User
Tags php write
This article mainly for you to analyze the PHP verification code class Validatecode, with a certain reference value, interested in small partners can refer to

PHP Parsing Verification Code class

1. Start

On the Internet to see the use of PHP write Validatecode generated verification code class, feel good, special take to analyze learning.

2. Class Diagram

3. Verification code Class part code

3.1 Defining variables

  Random factor  Private $charset = ' abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789 ';  Private $code;  Private $codeLen = 4;  Private $width =;  Private $heigh =;  private $img;//image  private $font;//font  private $fontsize = 20;

$charset is a random factor, here is a few characters that are not easily distinguishable, such as the letter "I,l,o,q", the number "0,1". It is necessary to add some Chinese or other characters or formulas.

$codeLen indicates the length of the CAPTCHA, common 4-bit.

3.2 constructor, set Captcha font, generate a true Color image img

Public function __construct ()  {    $this->font = Root_path. ' /font/chowderhead.ttf ';    $this->img = Imagecreatetruecolor ($this->width, $this->heigh);  }

3.3 Randomly extracts 4 characters from a random factor as a $code verification code.


Generate random Code  private Function Createcode ()  {    $_len = strlen ($this->charset)-1;    for ($i = 0; $i < $this->codelen; $i + +) {      $this->code. = $this->charset[mt_rand (0, $_len)];}  

3.4 Generate the CAPTCHA background color.

Generate background  private function createbg ()  {$color = imagecolorallocate ($this->img, Mt_rand (157, 255), Mt_rand ( 157, 255), Mt_rand (157, 255));    Imagefilledrectangle ($this->img, 0, $this->heigh, $this->width, 0, $color);  }

Among them Mt_rand (157, 255), the aim is to randomly take a relatively shallow color.

3.5 Create text on the image.

Generate text  private Function CreateFont ()  {    $_x = $this->width/$this->codelen;    $_y = $this->heigh/2;    for ($i = 0; $i < $this->codelen; $i + +) {      $color = imagecolorallocate ($this->img, Mt_rand (0, 156), Mt_rand (0, 156), Mt_rand (0, 156));      Imagettftext ($this->img, $this->fontsize, Mt_rand ( -30,), $_x * $i + mt_rand (3, 5), $_y + Mt_rand (2, 4), $color, $ This->font, $this->code[$i]);    }  }

Generate CAPTCHA text on the image, mainly considering the position of the text on the image and the color of each text.

Controls the X-axis position of nth Text = (image width/Verification code length) * (n-1) + random offset number; where n = {D1....N}

Controls the y-axis position of nth Text = Image Height/2 + random offset number;

Mt_rand (0, 156) randomly takes the text color, 0-156 is to take the darker color.

Mt_rand (-30, 30) random text rotation.

3.6 Creating lines and snowflakes on an image

Generate line, snowflake  private Function Createline ()  {for    ($i = 0; $i < $i + +) {      $color = imagecolorallocate ($th Is->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->heigh), Mt_rand (0, $this->width), Mt_ Rand (0, $this->heigh), $color);    }    for ($i = 0; $i <, $i + +) {      $color = imagecolorallocate ($this->img, Mt_rand (255), Mt_rand ($, 255), Mt_ RAND (255));      Imagestring ($this->img, Mt_rand (1, 5), Mt_rand (0, $this->width), Mt_rand (0, $this->heigh), ' # ', $color);    }  }

When drawing lines, take a darker color value, while drawing snowflakes to take a relatively light color value, the purpose is as far as possible does not affect the human eye identification verification code, but also can interfere with the automatic identification verification code mechanism.

3.7 Generate the CAPTCHA image externally for external invocation.

Externally generated public  function doimg ()  {    $this->createbg ();   1. Create the captcha background    $this->createcode ();  2. Generate random Code    $this->createline ();  3. Generate lines and snowflakes    $this->createfont ();  4. Generate Text    $this->output ();    5. Output Verification Code image  }

3.8 Complete Code:

<?php/** * Created by Phpstorm. * User:andy * date:16-12-22 * Time: PM 1:20 */class validatecode{//random factor private $charset = ' ABCDEFGHJKMNPRSTUVWXYZABCD  EFGJKMNPRSTUVWXYZ23456789 ';  Private $code;  Private $codeLen = 4;  Private $width = 130;  Private $heigh = 50;  private $img;//image private $font;//font private $fontsize = 20; Public Function __construct () {$this->font = Root_path. '    /font/chowderhead.ttf ';  $this->img = Imagecreatetruecolor ($this->width, $this->heigh);    }//Generate random Code Private Function Createcode () {$_len = strlen ($this->charset)-1;    for ($i = 0; $i < $this->codelen; $i + +) {$this->code. = $this->charset[mt_rand (0, $_len)];  }}//Generate background Private function createbg () {$color = Imagecolorallocate ($this->img, Mt_rand (157, 255), Mt_rand (157,    255), Mt_rand (157, 255));  Imagefilledrectangle ($this->img, 0, $this->heigh, $this->width, 0, $color); }//Generate text Private function CreateFont () {$_x = $this-> width/$this->codelen;    $_y = $this->heigh/2; for ($i = 0; $i < $this->codelen; $i + +) {$color = Imagecolorallocate ($this->img, Mt_rand (0, 156), Mt_rand (0      , 156), Mt_rand (0, 156)); Imagettftext ($this->img, $this->fontsize, Mt_rand ( -30,), $_x * $i + mt_rand (3, 5), $_y + Mt_rand (2, 4), $color, $    This->font, $this->code[$i]); }}//Generate line, snowflake Private Function Createline () {for ($i = 0; $i < $i + +) {$color = Imagecolorallocate ($th      Is->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->heigh), Mt_rand (0, $this->width), Mt_    Rand (0, $this->heigh), $color); } for ($i = 0; $i <, $i + +) {$color = Imagecolorallocate ($this->img, Mt_rand (255), Mt_rand (200, 255      ), Mt_rand (200, 255));    Imagestring ($this->img, Mt_rand (1, 5), Mt_rand (0, $this->width), Mt_rand (0, $this->heigh), ' # ', $color);  }}//Output imagePrivate Function OutPut () {header (' content-type:image/png ');    Imagepng ($this->img);  Imagedestroy ($this->img);   }//Externally generated public function doimg () {$this->createbg ();  1. Create the captcha background $this->createcode ();  2. Generate Random Code $this->createline ();  3. Generate lines and Snowflakes $this->createfont ();    4. Generate text $this->output ();  5. Output the CAPTCHA image}//Get the CAPTCHA public Function GetCode () {return strtolower ($this->code); }}

4. Testing

Test code:

<?php/** * Created by Phpstorm. * User:andy * date:16-12-22 * Time: PM 1:20 */define (' Root_path ', DirName (__file__)); require_once Root_path. ' /includes/validatecode.class.php '; $_vc=new validatecode (); Echo $_vc->doimg ();

Generate Verification Code:

5. Application

<label></label>

The above onclick code is click on the verification code image, can automatically refresh the verification code.

code.php:

<?php/** * Created by Phpstorm. * User:andy * date:16-12-22 * Time: PM 3:43 */require substr (dirname (__file__), 0,-7). ' /init.inc.php '; $_vc=new validatecode (); Echo $_vc->doimg (); $_session[' Validatecode ']=$_vc->getcode ();

6. Summary

During the independent testing process, no problem was found, but when applied to the project, it was just beginning to find that the verification code picture could not be generated, the web looked for it, some said in the output () function,

In the header (' content-type:image/png '); This line of code is preceded by an additional line of Ob_clean () code, which resolves the problem of captcha. Although this method is simple, this can cause other buffered data problems, because the Db_clean () function is to discard the contents of the output buffer.

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.