PHP verification code (latest modification, fully customized !) _ PHP Tutorial

Source: Internet
Author: User
PHP verification code (latest modification, fully customized !). The copy code for downloading Authnum. class. php is as follows :? Phpsession_start (); classAuthnum {image object, width, height, verification code length private $ im; private $ im_width; private Authnum. class. php download

The code is as follows:


Session_start ();
Class Authnum {
// Image object, width, height, and verification code length
Private $ im;
Private $ im_width;
Private $ im_height;
Private $ len;
// Random string, Y axis coordinate value, and random color
Private $ randnum;
Private $ y;
Private $ randcolor;
// The red, green, and blue background color. the default value is light gray.
Public $ red= 238;
Public $ green = 238;
Public $ blue = 238;
/**
* Optional settings: verification code type, interference point, interference line, and y-axis random
* If it is set to false, this parameter is disabled.
**/
// The default value is a combination of upper and lower case numbers. the value 1 2 3 indicates lower case, upper case, and number type respectively.
Public $ ext_num_type = '';
Public $ ext_pixel = false; // interference point
Public $ ext_line = false; // interference line
Public $ ext_rand_y = true; // random Y axis
Function _ construct ($ len = 4, $ im_width = '', $ im_height = 25 ){
// The verification code length, image width, and height are required for class instantiation.
$ This-> len = $ len; $ im_width = $ len * 15;
$ This-> im_width = $ im_width;
$ This-> im_height = $ im_height;
$ This-> im = imagecreate ($ im_width, $ im_height );
}
// Set the background color of the image. the default value is light gray.
Function set_bgcolor (){
Imagecolorallocate ($ this-> im, $ this-> red, $ this-> green, $ this-> blue );
}
// Obtain random digits
Function get_randnum (){
$ An1 = 'abcdefghijklmnopqrstuvwxy ';
$ An2 = 'abcdefghijklmnopqrstuvwxy ';
$ An3 = '20140901 ';
If ($ this-> ext_num_type = '') $ str = $ an1. $ an2. $ an3;
If ($ this-> ext_num_type = 1) $ str = $ an1;
If ($ this-> ext_num_type = 2) $ str = $ an2;
If ($ this-> ext_num_type = 3) $ str = $ an3;
For ($ I = 0; $ I <$ this-> len; $ I ++ ){
$ Start = rand (1, strlen ($ str)-1 );
$ Randnum. = substr ($ str, $ start, 1 );
}
$ This-> randnum = $ randnum;
$ _ SESSION [an] = $ this-> randnum;
}
// Obtain the verification code Image Y axis
Function get_y (){
If ($ this-> ext_rand_y) $ this-> y = rand (5, $ this-> im_height/5 );
Else $ this-> y = $ this-> im_height/4;
}
// Obtain random colors
Function get_randcolor (){
$ This-> randcolor = imagecolorallocate ($ this-> im, rand (0,100), rand (0,150), rand (0,200 ));
}
// Add interference points
Function set_ext_pixel (){
If ($ this-> ext_pixel ){
For ($ I = 0; I I <100; $ I ++ ){
$ This-> get_randcolor ();
Imagesetpixel ($ this-> im, rand () % 100, rand () % 100, $ this-> randcolor );
}
}
}
// Add interference lines
Function set_ext_line (){
If ($ this-> ext_line ){
For ($ j = 0; $ j <2; $ j ++ ){
$ Rand_x = rand (2, $ this-> im_width );
$ Rand_y = rand (2, $ this-> im_height );
$ Rand_x2 = rand (2, $ this-> im_width );
$ Rand_y2 = rand (2, $ this-> im_height );
$ This-> get_randcolor ();
Imageline ($ this-> im, $ rand_x, $ rand_y, $ rand_x2, $ rand_y2, $ this-> randcolor );
}
}
}
/** Create a verification code Image:
* Create a canvas (_ construct function)
* Set the canvas background ($ this-> set_bgcolor ();)
* Obtain a random string ($ this-> get_randnum ();)
* Writing text to an image (imagestring function)
* Add interference points/lines ($ this-> set_ext_line (); $ this-> set_ext_pixel ();)
* Output image
**/
Function create (){
$ This-> set_bgcolor ();
$ This-> get_randnum ();
For ($ I = 0; $ I <$ this-> len; $ I ++ ){
$ Font = rand (4, 6 );
$ X = $ I/$ this-> len * $ this-> im_width + rand (1, $ this-> len );
$ This-> get_y ();
$ This-> get_randcolor ();
Imagestring ($ this-> im, $ font, $ x, $ this-> y, substr ($ this-> randnum, $ I, 1 ), $ this-> randcolor );
}
$ This-> set_ext_line ();
$ This-> set_ext_pixel ();
Header ("content-type: image/png ");
Imagepng ($ this-> im );
Imagedestroy ($ this-> im); // release image resources
}
} // End class
/** Methods for using the verification code class:
* $ An = new Authnum (verification code length, image width, and image height );
* If the parameter is not included in the instantiation, the default value is a four-digit 60*25-size regular verification code image.
* Check the verification code on the form page. compare whether $ _ SESSION [an] is equal to $ _ POST [verification code text box ID].
* Optional configuration:
* 1. verification code type: $ an-> ext_num_type = 1. the value 1 indicates lower case, 2 indicates upper case, and 3 Indicates number.
* 2. Interference point: $ an-> ext_pixel = false; a value of false indicates no interference point is added.
* 3. Interference line: $ an-> ext_line = false; false indicates no interference line is added.
* 4. y-axis random: $ an-> ext_rand_y = false; if the value is false, y-axis random is not supported.
* 5. image background: change the value of the three member variables $ red $ green $ blue.
**/
$ An = new Authnum ();
$ An-> ext_num_type = '';
$ An-> ext_pixel = true; // interference point
$ An-> ext_line = false; // interference line
$ An-> ext_rand_y = true; // random Y axis
$ An-> green = 238;
$ An-> create ();
?>

The http://www.bkjia.com/PHPjc/322609.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322609.htmlTechArticleAuthnum.class.php download code is as follows :? Php session_start (); class Authnum {// image object, width, height, verification code length private $ im; private $ im_width; private...

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.