Practical PHP Verification Code (1/2)

Source: Internet
Author: User

Verification codes are indispensable in application development. We often encounter problems related to registration by machines, so the verification code can effectively prevent such behaviors. Let's take a look at the code I provided.

Verification codes are indispensable in application development. We often encounter problems related to registration by machines, so the verification code can effectively prevent such behaviors. Let's take a look at the code I provided.

<? Php tutorial
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;
}

1 2

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.