PHP Verification Code Class example

Source: Internet
Author: User
Tags rand

  This article mainly introduces a useful PHP verification code Class example, the need for friends can refer to the

Share a handy PHP authentication code class, including the call example. Note: If you do not apply the specified font, then use the imagestring () function, and if you need to encounter the specified font, use the Imagettftext () function. The position of the font is windows/fonts under C disk.   Reference to the online PHP generated verification code method, as well as PHP image verification code and PHP Chinese verification code generation method. Use of the PHP GD library related knowledge.   1, generate the Verification code class VerificationCode.class.php   code as follows: <?php       class verificationcode{  &nbs P       Private $charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";  //random factor           private $code;  //Verification Code           Private $codelen = 4; Verification code length           private $width = 110; Width           private $height = 30; Height           private $img;  //Image resource handle           private $font;  //set font           private $fontSize = 25;  //font size           private $fontColor; Font Color           public Function __construct () {  &nbsp           $this->font= "Calibriz. TTF ";           {         //Generate verification code           Private function Createcode () {              $len =strlen ($this->charset)-1;     & nbsp         for ($i = 0 $i < $this->codelen; $i + +) {            &NBS P     $this->code. = $this->charset[mt_rand (0, $len)];               I                     Generate background           Private Function createbg () {              $ This->img=imagecreatetruecolor ($this->width, $this->height);               $color = Imagecolorallocate ($this->img,mt_rand (157,255), Mt_rand ( 157,255), Mt_rand (157,255));              Imagefilledrectangle ($this->img,0, $this->height, $this->width,0, $color);           {         //Generate text           Private function CreateFont () {              $x = $this->width/$this->codelen;   &nbs P           for ($i = 0 $i < $this->codelen; $i + +) {          &NBS P       $this->fontcolor=imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand ( 0,156));                   Imagettftext ($this->img, $this->fontsize,mt_rand (- 30,30), $i * $x +mt_rand (1,5), $this->height/1.4, $this->fontcolor, $this->font, $this->code[$i]);  //www.jbxue.com                //imagestring ($this->img,5, $i * $x +mt_ Rand (1,5), 5, $this->code[$i], $this->fontcolor);              {         }          //Generate line, snowflake &NB Sp         Private Function createdisturb () {              for ($i = 0 ; $i < 6; $i + +) {                  $color =imagecolorallocate ($this->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->width), Mt_rand (0, $this->width), Mt_rand (0, $this->width), $color);               $i               for ($i = 0; < 1 00; $i + +) {                  $color =imagecolorallocate ($this->img,mt_rand (20 0,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->height), ' * ', $color);               I                     Output           Private Function OutPut () {              Heade R ("Content-type:image/png");               imagepng ($this->img);               Imagedestroy ($this->img);           {          public Function ShowCode () {      & nbsp       $this->CREATEBG ();               $this->createcode ();               $this->createdisturb ();               $this->createfont ();               $this->output ();         &NBsp          //get validation code           public Function GetCode () {    & nbsp         return Strtolower ($this->code);           {     }  ?>     code.php     code as follows: <?p HP       session_start ();       require_once ' VerificationCode.class.php ';       $code =new verificationcode ();       $_session[' code ']= $code->getcode ();       $code->showcode ();  ?>     Verification code: <input type= "text" name= "code"/><img src= "code.php" onclick= "javascript: This.src= ' code.php?time= ' +math.random (); "/>  

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.