Use php to generate verification codes for numbers and letters (1) and php verification Codes

Source: Internet
Author: User

Use php to generate verification codes for numbers and letters (1) and php verification Codes

Projects often encounter a series of security verification policies, such as login verification, payment verification, and so on. There are various implementation methods. Next we will explain how to use php to generate a simple text + digit combination verification code:

Php and gd Libraries

Principles:

A> in essence, a verification code is randomly generated on the server and stored in $ _ SESSION.

B> then, write the verification code on the image and send the image to the client. The user enters the verification code on the image and submits it to the server.

C> the server compares with the information stored in $ _ SESSION. If the information is consistent, the request passes. Otherwise, the request fails.

 

 

There are two steps:

1) Step 1: generate a verification code on the server

2) The client uses the verification code

Step 1: how to generate a verification code on the server: <code. php>

<? Phpsession_start ();
// 1> session_start () must be placed at the top, and 2> multiple servers. You must manage session information in a centralized manner. // phpinfo (); // print php information.
// 1. generate a basemap (width: 100; height: 30) $ image = imagecreatetruecolor (255,255,255); $ bgcolor = imagecolorallocate ($ image,); // paint brush coloring imagefill ($ image, 0, 0, $ bgcolor); // color the base map. // four numbers are randomly generated./* for ($ I = 0; $ I <4; $ I ++) {$ fontcolor = imagecolorallocate ($ image, rand (0,120), rand (0,120), rand (0,120); $ fontcontent = rand ); // content of the Verification Code $ fontsize = 6; // font size $ x = ($ I * 100/4) + rand (5, 10); // x axis $ y = rand (5, 10 ); // y-axis imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor); // use imagestring () write the text on the canvas} */$ captch_code = ""; for ($ I = 0; $ I <4; $ I ++) {$ fontsize = 6; $ fontcolor = imagecolorallocate ($ image, rand (20,100), rand (30,100), rand (10,200); $ str = "abcdefghkmnpwsert1234567890"; // returns a string, used to generate a Random verification code $ fontcontent = substr ($ str, rand (0, strlen ($ str), 1); // capture a character $ captch_code each time. = $ fontcontent; // splice $ x = ($ I * 100/4) + rand (); $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor );}$ _ SESSION ['authcode'] = $ captch_code;// Save it to the session // Add the interference point for ($ I = 0; $ I <100; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,200), rand (50,200), rand (50,200); imagesetpixel ($ image, rand (0,100), rand (), $ pointcolor );} // Add interference lines for ($ I = 0; $ I <3; $ I ++) {$ linecolor = imagecolorallocate ($ image, rand (80,200 ), rand (80,200), rand (80,200); imageline ($ image, rand (5, 95), rand (5, 25), rand (5, 95), rand (5, 25 ), $ linecolor );}
Ob_clean (); // clear the cache header ("content-type :". 'image/png '); // fixed format of the output image header file (which can be image/jpg, image/png, or) imagepng ($ image ); // output an image imagedestroy ($ image) to the browser; // destroy the basemap in the memory // session_start

Step 2: return the generated Verification Code image to the client <form. php>

<? Phpheader ("Content-type: text/html; charset = UTF-8"); // if (isset ($ _ REQUEST ['authcode']) to avoid garbled characters) {session_start (); // before using $ _ SESSION, session_start () if (strtolower ($ _ REQUEST ['authcode']) must be used. ==$ _ SESSION ["authcode"]) {// $ _ SESSION ['authcode'] is the verification code stored on the server, $ _ REQUEST ['authcode'] obtain the information entered by the client echo "<font color = '# 000cc'> the input is correct </font> ";} else {echo "<font color = '# 0000cc'> input error </font>" ;}exit () ;}?>
<! DOCTYPE html> // Click the verification code image to switch the verification code (requirement 1) // generate a random number to facilitate access to the server, if the address is the same, the browser will not refresh the access  "Width =" 100px; height: 100px; "alt =" "onclick =" this. src = 'Code. php? + Math. random () '"> // click 'Switch click' to switch the verification code (requirement 2)  <? Php // echo rand ();?> <! -- "Width =" 100px; height: 100px; "alt =" "> <a href =" javascript: void (0) "onclick =" document. getElementById ('captcode '). src = 'Code. php? = <? Php echo rand ();?> '"> Click to switch? </A> </p> <p> enter the verification code: <input type = "text" name = "authcode" value = ""/> </p> <P> <input type = "submit" value = "submit">

 

 

  

 

  

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.