Network verification code automatically generated program-php Tutorial

Source: Internet
Author: User
The network verification code automatically generates a program. Where does the network verification code automatically generate a program? How to join php?


Reply to discussion (solution)

Use google directly.
Https://developers.google.com/recaptcha/

Search for more information on the Internet.

Use google directly.
Https://developers.google.com/recaptcha/
Is it okay to call google's api?

Yes. the security factor for calling google APIs is quite high.



Class captcha {
//
Private $ captcha_key = 'captcha _ code'; // subscript of the element in the session
Private $ width; // image width
Private $ height; // Image height

/**
* Constructor
* @ Param $ width integer width
* @ Param $ height integer height
*/
Public function _ construct (){
}

Public function generate ($ width, $ height ){
// Initialize attributes
$ This-> width = $ width;
$ This-> height = $ height;
// Create a verification code
$ Chars = array_merge (range ('A', 'z'), range ('A', 'z'), range ('1', '9 '));
$ Rand_keys = array_rand ($ chars, 4 );
Shuffle ($ rand_keys );
Foreach ($ rand_keys as $ key ){
$ Char [] = $ chars [$ key];
}

// Put it into the session
@ Session_start ();
// Connect the string and save it to the session
// Implode (). If the first parameter is not specified, it indicates a null character connection.
$ _ SESSION [$ this-> captcha_key] = implode ($ char );


// Process the image
// Step 1
$ Img = imagecreatetruecolor ($ this-> width, $ this-> height );
// Step 2
// Fill in the background
// First design the color and assign it to the image.
$ Backgroud_color = imagecolorallocate ($ img, mt_rand (180,255), mt_rand (180,255), mt_rand (180,255 ));
// Fill in the image
Imagefill ($ img, 0, 0, $ backgroud_color );


// Add some interference lines
// First assign the line segment color
For ($ I = 0; $ I <5; $ I ++ ){
$ Line_color = imagecolorallocate ($ img, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 ));
// Draw a line
Imageline ($ img, mt_rand (0, $ this-> width-1), mt_rand (0, $ this-> height-1), mt_rand (0, $ this-> width-1), mt_rand (0, $ this-> height-1), $ line_color );
}

// Add interference pixels
For ($ I = 0; I I <100; $ I ++ ){
$ Pixel_color = imagecolorallocate ($ img, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 ));
Imagesetpixel ($ img, mt_rand (0, $ this-> width-1), mt_rand (0, $ this-> height-1), $ pixel_color );
}




// Obtain the colors that can be used on resources first
$ String_color = imagecolorallocate ($ img, mt_rand (80,150), mt_rand (80,150), mt_rand (80,150 ));//
// Write the obtained string to the image
Imagestring ($ img, 5, 55, 2, $ _ SESSION [$ this-> captcha_key], $ string_color );
// Step 3
// Set the response header. the content to be output to the browser is in png format.
Header ('content-Type: image/png ');
Imagepng ($ img); // if there is no second parameter, it indicates the meaning of the output.
// Step 4
Imagedestroy ($ img );

}

/**
* Determine whether the verification code submitted by the user is correct
* @ Param $ code string data submitted by the user
* @ Return: If bool is consistent, true is returned. if not, false is returned.
*/
Public function checkCaptcha ($ code ){
Return strtoupper ($ code) = strtoupper ($ _ SESSION [$ this-> captcha_key]);
}
}

For details about how to use it, you can download and view Han Shunping's video tutorial 120th session ④-verification code to prevent malicious attacks. The above code is down from the lecture notes.



Class captcha {
//
Private $ captcha_key = 'captcha _ code'; // subscript of the element in the session
Private $ width; // image width
Private $ height; // Image height

/**
* Constructor
* @ Param $ width integer width
* @ Param $ height integer height
*/
Public function _ construct (){
}

Public function generate ($ width, $ height ){
// Initialize attributes
$ This-> width = $ width;
$ This-> height = $ height;
// Create a verification code
$ Chars = array_merge (range ('A', 'z'), range ('A', 'z'), range ('1', '9 '));
$ Rand_keys = array_rand ($ chars, 4 );
Shuffle ($ rand_keys );
Foreach ($ rand_keys as $ key ){
$ Char [] = $ chars [$ key];
}

// Put it into the session
@ Session_start ();
// Connect the string and save it to the session
// Implode (). If the first parameter is not specified, it indicates a null character connection.
$ _ SESSION [$ this-> captcha_key] = implode ($ char );


// Process the image
// Step 1
$ Img = imagecreatetruecolor ($ this-> width, $ this-> height );
// Step 2
// Fill in the background
// First design the color and assign it to the image.
$ Backgroud_color = imagecolorallocate ($ img, mt_rand (180,255), mt_rand (180,255), mt_rand (180,255 ));
// Fill in the image
Imagefill ($ img, 0, 0, $ backgroud_color );


// Add some interference lines
// First assign the line segment color
For ($ I = 0; $ I <5; $ I ++ ){
$ Line_color = imagecolorallocate ($ img, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 ));
// Draw a line
Imageline ($ img, mt_rand (0, $ this-> width-1), mt_rand (0, $ this-> height-1), mt_rand (0, $ this-> width-1), mt_rand (0, $ this-> height-1), $ line_color );
}

// Add interference pixels
For ($ I = 0; I I <100; $ I ++ ){
$ Pixel_color = imagecolorallocate ($ img, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 ));
Imagesetpixel ($ img, mt_rand (0, $ this-> width-1), mt_rand (0, $ this-> height-1), $ pixel_color );
}




// Obtain the colors that can be used on resources first
$ String_color = imagecolorallocate ($ img, mt_rand (80,150), mt_rand (80,150), mt_rand (80,150 ));//
// Write the obtained string to the image
Imagestring ($ img, 5, 55, 2, $ _ SESSION [$ this-> captcha_key], $ string_color );
// Step 3
// Set the response header. the content to be output to the browser is in png format.
Header ('content-Type: image/png ');
Imagepng ($ img); // if there is no second parameter, it indicates the meaning of the output.
// Step 4
Imagedestroy ($ img );

}

/**
* Determine whether the verification code submitted by the user is correct
* @ Param $ code string data submitted by the user
* @ Return: If bool is consistent, true is returned. if not, false is returned.
*/
Public function checkCaptcha ($ code ){
Return strtoupper ($ code) = strtoupper ($ _ SESSION [$ this-> captcha_key]);
}
}

For details about how to use it, you can download and view Han Shunping's video tutorial 120th session ④-verification code to prevent malicious attacks. The above code is down from the lecture notes.

Q: What should I do if I want to generate code similar to google?

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.