Here there are two verification code generation programs. The first one is better, and the second one is also good. At the same time, the second generation program uses an instance to describe the call and generation of the verification code.
Here there are two verification code generation programs. The first one is better, and the second one is also good. At the same time, the second generation program uses an instance to describe the call and generation of the verification code. */
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 ();
?>
Let's take a look at a verification code call instance.
Demo:
Reference content is as follows:
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Title> hi.baidu.com/ji_haiyang </title>
</Head>
<Body>
<Form action = "demo1.php tutorial" method = "post" name = "form1">
<Input name = "test" type = "text" value = "1234"/>
<Div onclick = 'this. innerhtml = " </img> "'>
</img>
</Div>
<Input name = "test2" type = "text" value = "1"/>
<Input name = "submit" type = "submit" value = "submit"/>
</Form>
</Body>
</Html>
Example demo1:
<? Php
Session_start ();
$ Test = $ _ post ['test'];
$ Test = trim ($ test );
$ Submit = $ _ post ['submit '];
If ($ test ==$ _ session ['vcode']) {
Echo 'true, correct Verification Code ';
} Else {
Echo 'false, incorrect Verification Code ';
}
?>
Call the file vcode. php: The reference content is as follows:
<? Php
/**
* The default Verification Code session is vcode. That is, $ _ session ['vcode'];
* Do not conflict the variable name with the session when giving the variable value.
* Note: It is case-insensitive during verification.
*/
Include ("inc_vcode_class.php ");
$ V = new vcode;
// $ Config ['width'] = 50; // Verification Code width
// $ Config ['height'] = 20; // high Verification Code
// $ Config ['vcode'] = "vcode"; // The session used to check the verification code
// $ Config ['type'] = "default"; // Verification Code display type default: uppercase letters, string: lowercase letters, int: Number php programmer site
// $ Config ['length'] = 4; // verification code length
// $ Config ['interfere'] = 10; // interference line intensity, ranging from 1 to 30, 0, or no interference line available
// $ V-> init ($ config); // configure
$ V-> create ();
?>
Verification Code class inc_vcode_class.php: www ~ Phperz ~ Com:
<? Php
/**
* Verification Code
* Note: gd database support is required.
*/
Session_start ();
Class vcode {
Private $ config;
Private $ im;
Private $ str;
Function _ construct (){
$ This-> config ['width'] = 50;
$ This-> config ['height'] = 20;
$ This-> config ['vcode'] = "vcode ";
$ This-> config ['type'] = "default ";
$ This-> config ['length'] = 4;
$ This-> config ['interfere'] = 10;
$ This-> str ['default'] = "abcdefghijklmnopqrstuvwxyz ";
$ This-> str ['string'] = "abcdefghijklmnopqrstuvwxyz ";
$ This-> str ['int'] = "0123456789 ";
}
// Configure
Public function init ($ config = array ()){
If (! Empty ($ config) & is_array ($ config )){
Foreach ($ config as $ key => $ value ){
$ This-> config [$ key] = $ value;
}
}
}
// Output the verification code
Public function create (){
If (! Function_exists ("imagecreate ")){
Return false;
}
$ This-> create_do ();
}
// Create
Private function create_do (){
$ This-> im = imagecreate ($ this-> config ['width'], $ this-> config ['height']); php programmer's house
// Set the background to white
Imagecolorallocate ($ this-> im, 255,255,255 );
// Add a border for the background
$ Bordercolor = imagecolorallocate ($ this-> im, 37,37, 37 );
Imagerectangle ($ this-> im, 0, 0, $ this-> config ['width']-1, $ this-> config ['height']-1, $ bordercolor );
// Generate the verification code
$ This-> create_str ();
$ Vcode = $ _ session [$ this-> config ['vcode'];
// Input text
$ Fontcolor = imagecolorallocate ($ this-> im, 6,46, 46 );
For ($ I = 0; $ I <$ this-> config ['length']; $ I ++ ){
Imagestring ($ this-> im, 5, $ I * 10 + 6, rand (2, 5), $ vcode [$ I], $ fontcolor );
}
// Add interference lines
$ Interfere = $ this-> config ['interfere'];
$ Interfere = $ interfere> 30? "30": $ interfere; php programmer site
If (! Empty ($ interfere) & $ interfere> 1 ){
For ($ I = 1; $ I <$ interfere; $ I ++ ){
$ Linecolor = imagecolorallocate ($ this-> im, rand (0,255), rand (0,255), rand (0,255 ));
$ X = rand (1, $ this-> config ['width']);
$ Y = rand (1, $ this-> config ['height']);
$ X2 = rand ($ X-10, $ x + 10 );
$ Y2 = rand ($ y-10, $ y + 10 );
Imageline ($ this-> im, $ x, $ y, $ x2, $ y2, $ linecolor );
}
}
Header ("pragma: no-cachern ");
Header ("cache-control: no-cachern ");
Header ("expires: 0rn ");
Header ("content-type: image/policrn ");
Imagejpeg ($ this-> im );
Imagedestroy ($ this-> im );
Exit;
}
// Obtain the verification code
Private function create_str (){
If ($ this-> config ['type'] = "int "){
For ($ I = 1; $ I <= $ this-> config ['length']; $ I ++ ){
$ Vcode. = rand (0, 9 );
}
$ _ Session [$ this-> config ['vcode'] = $ vcode;
Return true;
}
$ Len = strlen ($ this-> str [$ this-> config ['type']);
If (! $ Len ){
$ This-> config ['type'] = "default ";
$ This-> create_str ();
}
For ($ I = 1; $ I <= $ this-> config ['length']; $ I ++ ){
$ Offset = rand (0, $ len-1 );
$ Vcode. = substr ($ this-> str [$ this-> config ['type'], $ offset, 1 );
}
$ _ Session [$ this-> config ['vcode'] = $ vcode;
Return true;
}
}