How to implement the verification code in php. how to implement the verification code in php?

Source: Internet
Author: User
Teach you how to implement the verification code in php and implement the verification code in php. It is inconvenient to teach you how to implement a verification code in php. more and more verification codes are implemented in forms, so I learned how to implement the verification code in php and how to implement the verification code in php.

More and more verification codes are implemented in forms, but it is inconvenient to use the verification codes written in js. Therefore, I learned the verification codes implemented by php. Well, there is nothing to do, but I don't want to waste time, so I learned how to implement the verification code in php. This is what we call it. In addition, it can also be encapsulated into a function, which will be convenient to use in the future. of course, it is not encapsulated now.

Now let's talk about a simple pure digital verification code.

If you are a beginner, we recommend that you follow the notes/numbers in my code step by step. The simplest method is to copy the entire code.

Create a new captcha. php:

Php // 10> set the session, which must be at the top of the script session_start (); $ image = imagecreatetruecolor (100, 30 ); // 1> function for setting the verification code image size // 5> set the verification code color imagecolorallocate (int im, int red, int green, int blue); $ bgcolor = imagecolorallocate ($ image, 255,255,255); // # ffffff // 6> fill in the area where int imagefill (int im, int x, int y, int col) (x, y) is located, col indicates the color of imagefill ($ image, 0, 0, $ bgcolor) to be coated. // 10> set the variable $ captcha_code = ""; // 7> generate random numbers for ($ I = 0; $ I <4; $ I ++) {// set the font size $ fontsize = 6; // set the font color, random color $ fontcolor = imagecolorallocate ($ image, rand (0,120), rand (0,120), rand (0,120 )); // 0-120 deep color // set the number $ fontcontent = rand (); // 10>. = continuous definition variable $ captcha_code. = $ fontcontent; // Set coordinates $ x = ($ I * 100/4) + rand (); $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor);} // 10> save to session $ _ SESSION ['authcode'] = $ captcha_code; // 8> add interference elements, set snowflake points for ($ I = 0; $ I <200; $ I ++) {// set the color of the point. The color of 50-is lighter than the number, read $ pointcolor = imagecolorallocate ($ image, rand (50,200), rand (50,200), rand (50,200); // imagesetpixel-draw a single pixel imagesetpixel ($ image, rand (), rand (), $ pointcolor);} // 9> add interference elements and set the horizontal line for ($ I = 0; $ I <4; $ I ++) {// set the line color $ linecolor = imagecolorallocate ($ image, rand (80,220), rand (80,220), rand (80,220 )); // set the line, 2.1 line imageline ($ image, rand (), $ linecolor );} // 2> set the header, image/png header ('content-Type: image/png '); // 3> imagepng () create a png image function imagepng ($ image ); // 4> imagedestroy () end graph function destroy $ image imagedestroy ($ image );

The code for the static page is: index.html

       
      
  Confirm the verification code title> head>    
        
   

Verification code: 'style = "width: 100px; height: 30px"/> for another? A> p>

Enter the verification code:P>

P>

As you can see from index.html, the submitted form is in form. php, so there is also a judgment form. php code:

Php header ("Content-Type: text/html; charset = utf-8"); // Set the header information // isset () check if the variable is set if (isset ($ _ REQUEST ['authcode']) {session_start (); // strtolower () lower case function if (strtolower ($ _ REQUEST ['authcode']) ==$ _ SESSION ['authcode']) {// jump to the page echo"

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.