Verification codes are increasingly implemented in forms, but it is inconvenient to use the verification codes written in js. so I learned the principles and implementation of the verification codes implemented by php, if you are interested in the knowledge of the verification code generation principle, you can learn about it. more and more verification codes are implemented in forms. However, it is inconvenient to use verification codes written in js, so I learned the verification code 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.
Here is an attachment:
Because the registration code is often used to prevent malicious registration by machines during registration, here I am posting a basic image that generates a png image verification code, which is simple but clear:
1. generate a png image
2. set the background color for the image
3. set the font color and style
4. generate a 4-digit random verification code
5. adjust the rotation angle and position of each generated character to the png image.
6. add noise and interference lines to prevent registration machines from malicious registration by analyzing original images
7. output image
8. release memory occupied by images
Authcode. php file
Code
<? Phpsession_start (); header ('content-type: image/png '); // create an image $ im = imagecreate ($ x = 130, $ y = 45 ); $ bg = imagecolorallocate ($ im, rand (50,200), rand (0,155), rand (0,155); // The first time on imagecolorallocate () the call will fill the background color $ fontColor = imageColorAllocate ($ im, 255,255,255) for the color palette-based image; // fontstyle = 'rock. ttf'; // the font style, which can be found in the c: \ windows \ Fonts \ folder. I put it in the and authcode. the PHP file is in the same directory. you can replace other font styles here // Generate random characters for ($ I = 0; $ I <4; $ I ++) {$ randAsciiNumArray = array (rand (48,57), rand (65,90); $ randAsciiNum = $ randAsciiNumArray [rand (0, 1)]; $ randStr = chr ($ randAsciiNum); imagettftext ($ im, 30, rand ()-rand (), 5 + $ I * 30, rand ), $ fontColor, $ fontstyle, $ randStr); $ authcode. = $ randStr;} $ _ SESSION ['authcode'] = $ randFourStr; // compare the verification code entered by the user with that entered by the user // interfere with the line for ($ I = 0; $ I <8; $ I ++) {$ lineColor = imagecolorallocate ($ im, rand (0,255), rand (0,255), Rand (0,255); imageline ($ im, rand (0, $ x), 0, rand (0, $ x), $ y, $ lineColor );} // interference point for ($ I = 0; $ I <250; $ I ++) {imagesetpixel ($ im, rand (0, $ x), rand (0, $ y), $ fontColor);} imagepng ($ im); imagedestroy ($ im);?>
The above are related knowledge about the principles and implementation of PHP verification code generation, and I hope to help you.