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