This article mainly introduces php verification code generation. it takes only four steps to create a verification code basemap, display the verification code content, add interference elements, and output the verification code, next, let's split the steps. if you need a friend, you can refer to the verification code below, which is usually used to ensure the security of our website registration or login is not injected, however, 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.