Php verification code generation, php verification code generation _ PHP Tutorial

Source: Internet
Author: User
Php verification code generation, php verification code generation. Php verification code generation. php verification code generation verification code is usually used to ensure security of our website registration or login is not injected, but for more security, we usually generate some mixed verification php verification code generation code, php verification code generation

The verification code is usually used to ensure the security of website registration or login is not injected, but for the sake of security, we usually generate some mixed verification codes. let's take a look at the example below.

When we develop login modules or forum irrigation modules, we need to use verification codes to prevent malicious submission. verification codes are a means to distinguish people from machines. of course, this method is not foolproof, but it will always play a role.

The implementation of the verification code requires the support of the GD Library. if the GD library is not enabled for the children's shoes, the GD library must be enabled. In fact, the production and use of the verification code is very simple. it can be done in just four steps: create a verification code basemap, display the verification code content, add interference elements, and output the verification code. next we will split the process:

Step 1: Create a verification code Basemap

$ Image = imagecreatetruecolor (100, 30); // Create a basemap with a width of 100 and a height of 30. the background color of the basemap is black, which is defined by the system. $ bgcolor = imagecolorallocate ($ image, 255,255,255); // assign the white background color imagefill ($ image, 0, 0, $ bgcolor) to the basemap created above; // fill in the white background color

Step 2: Display the verification code content

// Output the verification code content for ($ I = 0; $ I <4; $ I ++) {$ fontsize = 6; $ fontcolor = imagecolorallocate ($ image, rand (0,120), rand (0,120), rand (0,120); $ data = 'qwertyuipkjhgfdsazxcvbnm23456789 '; $ content = substr ($ data, rand (0, strlen ($ data), 1); $ x = ($ I * 100/4) + rand (); $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ content, $ fontcolor); // outputs a row of strings horizontally on the image}

Step 3: Add interference elements

// Add the interference point element for ($ I = 0; $ I <300; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,200 ), rand (50,200), rand (50,200); imagesetpixel ($ image, rand (), rand (), $ pointcolor);} // open source software: phpfensi.com // the color of the element line and point added to the interference line must be better controlled than the color of the verification code number to avoid the phenomenon that the verification code number is invisible ($ I = 0; $ I <4; $ I ++) {$ linecolor = imagecolorallocate ($ image, rand (100,240), rand (100,240), rand (100,240); imageline ($ image, rand (), $ linecolor );}

Step 4: output the verification code

// Before outputting the created image, the output header must be used to specify the output image Type header ("Content-Type: image/png"); imagepng ($ image ); // destroy the image imagedestroy ($ image );

So far, a simple verification code has been implemented. the precautions for implementing the verification code have been written in a comment. when using the verification code, we generally need to use session to save it for verification, this document will not be detailed here.

The above is the php verification code generation code, which has been commented out in the important code. you can study the comment content carefully. it is also very important. I hope you can get something from it.

The authorization verification code is usually used to ensure that our website registration or login is not injected, but for the sake of security, we usually generate some mixed verification...

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.