PHP implements verification code and php Verification Code

Source: Internet
Author: User

PHP implements verification code and php Verification Code

The verification code is implemented using the SESSION function in PHP.
Declare the function session_start () at the top, telling the server that we want to use the function.

session_start(); 

Next we will use the verification code. Here we use the code of an English number as an example.

$ Image = imagecreatetruecolor (100, 30); // create a 100 × 30 canvas $ white = imagecolorallocate ($ image, 255,255,255); // white imagefill ($ image, $ white); // cover the Black canvas

Then, declare an empty variable before the verification code is implemented to store the verification code.

$ Session = ""; // empty variable, containing the verification code for ($ I = 0; $ I <4; $ I ++) {$ size = 6; $ x = $ I * 25 + mt_rand (80,220); $ y = mt_rand (80,220); $ sizi_color = imagecolorallocate ($ image, mt_rand (), mt_rand ), mt_rand (80,220); $ char = join ("", array_merge (range ('A', 'z'), range ('A', 'z '), range (); $ char = str_shuffle ($ char); $ char = substr ($ char,); imagestring ($ image, $ size, $ x, $ y, $ char, $ sizi_color); $ session. = $ char; // assign each value of the verification code to the variable} $ _ SESSION ['session'] = $ session; // the value of this variable is equal to the value entered by the user.
For ($ k = 0; $ k <200; $ k ++) {$ rand_color = imagecolorallocate ($ image, mt_rand (50,200), mt_rand (50,200 ), mt_rand (50,200); imagesetpixel ($ image, mt_rand (), mt_rand (), $ rand_color);} for ($ n = 0; $ n <5; $ n ++) {$ line_color = imagecolorallocate ($ image, mt_rand (80,220), mt_rand (80,220), mt_rand (80,220); imageline ($ image, mt_rand ), mt_rand (), $ line_color);} header ('content-type: image/png '); // set the file output format imagepng ($ image); // output $ image imagedestroy ($ image) in png format; // destroy the image

Use the POST method to receive the verification code. The strtolower function makes the server case insensitive. This effectively reduces the error rate.

If (isset ($ _ POST ['session']) {session_start (); if (strtolower ($ _ POST ['session']) = strtolower ($ _ SESSION ['session']) {echo '<font color = "# 0000CC"> the input is correct </form> ';} else {echo '<font color = "# CC0000"> <B> input error </B> </font>' ;}exit ();}

The following is the HTML page code.

<! DOCTYPE html> 

Here is a special description of the event onclick added to the HTML code. When the user cannot identify the current verification code, he does not need to refresh the browser and directly click "cannot see clearly? Replace the verification code with another one.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.