PHP production verification code, php Verification Code

Source: Internet
Author: User

PHP production verification code, php Verification Code

PHP Verification Code detailed tutorial

Effect:

Myvcode. class. php: class that encapsulates the verification code

   1: <?php
   2: /*
   3: * file:myvcode.class.php
4: * Verification Code Class, Class Name Vcode
   5: */
   6: class Vcode
   7: {
8: private $ width;/* Verification Code width */
9: private $ height;/* Verification Code height */
10: private $ codeNum;/* number of characters in the Verification Code */
11: private $ checkCode;/* Verification Code character */
12: private $ image;/* Verification Code resource */
13: private $ pixNum;/* Number of interference points to be drawn */
14: private $ lineNum;/* number of lines that draw interference lines */
  15:
  16:     /*
17: * constructor instantiate the verification code object and initialize data
18: * @ param int $ width: Set the default width.
19: * @ param int $ height: Set the default height.
20: * @ param int $ codeNum: set the number of characters in the verification code
21: * @ param int $ pixNum: set the number of interference points.
22: * @ param int $ lineNum: set the number of interference lines.
  23:     */
  24:     function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
  25:     {
  26:         $this->width = $width;
  27:         $this->height = $height;
  28:         $this->codeNum = $codeNum;
  29:         $this->pixNum = $pixNum;
  30:         $this->lineNum = $lineNum;
  31:     }
32:/* internal private method, creating image resources */
  33:     private function getCreateImage()
  34:     {
  35:         $this->image = imagecreatetruecolor($this->width, $this->height);
  36:         $white = imagecolorallocate($this->image,0xff,0xff,0xff);
  37:         imagefill($this->image, 0, 0, $white);
  38:         $black = imagecolorallocate($this->image,0,0,0);
  39:         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
  40:     }
41:/* internal private method, draw characters, remove o0Llz and 012 */
  42:     private function createCheckCode()
  43:     {
  44:         $code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
  45:         $this->checkCode = "";
  46:         for($i=0; $i<$this->codeNum;$i++)
  47:         {
  48:             $char = $code{rand(0,strlen($code) - 1)};
  49:             $this->checkCode .= $char;
  50:             $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
  51:             $fontSize = rand(3,5);
  52:             $x = rand(0,$this->width-imagefontwidth($fontSize));
  53:             $y = rand(0,$this->height-imagefontheight($fontSize));
  54:             imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
  55:         }
  56:     }
57:/* Set interference elements in the internal private Method */
  58:     private function setDisturbColor()
  59:     {
60:/* draw interference points */
  61:         for($i=0; $i<$this->pixNum; $i++)
  62:         {
  63:             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
  64:             imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
  65:         }
66:/* draw interference lines */
  67:         for($i=0; $i<$this->lineNum; $i++)
  68:         {
  69:             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));

70: imageline ($ this-> image, rand (1, $ this-> width/2), rand (1, $ this-> height/2 ),

rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);

  71:         }
  72:     }
73:/* enable session storage and use echo to output the image */
  74:     function __toString()
  75:     {
  76:         $_SESSION['code'] = strtoupper($this->checkCode);
  77:         $this->getCreateImage();
  78:         $this->createCheckCode();
  79:         $this->setDisturbColor();
  80:         $this->outputImg();
  81:     }
82:/* internal private method output image */
  83:     private function outputImg()
  84:     {
  85:         header("content-type:image/png");
  86:         imagepng($this->image);
  87:     }
88:/* destructor, release object */
  89:     function __destruct()
  90:     {
  91:         imagedestroy($this->image);
  92:     }
  93: }
  94: ?>

Imgcode. php output image

   1: <?php
   2: session_start();
   3: require_once('myvcode.class.php');
   4: echo new Vcode();
   5: ?>

Test.html: Same as img Tag reference

   1: 

You can add a tag and use js to achieve another effect:

/* Partial refresh for verification code */function changeCode () {var imgcode = document. getElementById ('code'); var change = document. getElementById ('change'); change. onclick = function () {/* You must add the following parameters to refresh */imgcode. src = 'Code. php? Tm '+ Math. random ();}}


Code and change are the IDs of img and a respectively.

 


PHP verification code creation Problems

When you use [pic], PHP will check whether there is a constant named pic. This cannot be found, so it will output a warning message at the beginning of the file. This information is output before the image data, and the image cannot be recognized by the browser. The solution is to quote the pic, $ _ SESSION ['pic ']

Php image verification code implementation

Php GD library can be used

// Generate a random Verification Code
Class randomString
{

Function createRandomStr ($ strLen)
{
List ($ usec, $ sec) = explode ('', microtime ());
(Float) $ sec + (float) $ usec * 100000 );

$ Number = '';
$ Number_len = $ strLen;
$ Stuff = '1234567890abcdefghijklmnopqrstuvwxyz '; // The display range of the additional code ABCDEFGHIJKLMNOPQRSTUVWXYZ
$ Stuff_len = strlen ($ stuff)-1;
For ($ I = 0; $ I <$ number_len; $ I ++ ){
$ Number. = substr ($ stuff, mt_rand (0, $ stuff_len), 1 );
}
Return $ number;
}
}
Use the ZD library to convert the verification code into an image.
$ Number = $ createStr-> createRandomStr ('4'); // number of digits of the Verification Code
$ Number_len = strlen ($ number );
$ _ SESSION ["VERIFY_CODE"] = $ number;

// Generate a verification code Image
$ Img_width = 60;
$ Img_height = 20;

$ Img = imageCreate ($ img_width, $ img_height );
ImageColorAllocate ($ img, 0x6C, 0x74, 0x70 );
$ White = ImageColorAllocate ($ img, 0xff, 0xff, 0xff );

$ Ix = 6;
$ Iy = 2;
For ($ I = 0; $ I <$ number_len; $ I ++ ){
ImageString ($ img, 5, $ ix, $ iy, $ number [$ I], $ white );
$ Ix + = 14;
}
For ($ I = 0; $ I <200; $ I ++) // Add interference pixels
{
$ Randcolor = ImageColorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ img, rand () % 100, rand () % 50, $ randcolor );
}

// Output image
Header ("Content-type:". image_type_to_mime_type (IMAGETYPE_PNG ));

Imagepng ($ img );
Imagedestroy ($ img);... remaining full text>

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.