PHP verification code generation principle and implementation, php verification code principle. PHP verification code generation principle and implementation, php verification code principle verification code is more and more in form implementation, but the verification code written using js is always inconvenient, so I learned how php verification code is generated and implemented based on PHP, and how php verification code works.
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.
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.
Articles you may be interested in:
- A beautiful php verification code class (share)
- Solution to "the image cannot be displayed due to its own mistakes" when PHP generates the verification code
- Summary of several methods for generating graphical verification codes using php
- Share a handy PHP verification code instance
- Simple ThinkPHP verification code tutorial
- Thinkphp verification code cannot be displayed
- PHP generates image verification codes and click to switch instances
- PHP uses CURL to simulate logon to websites with verification codes
Pipeline verification codes are increasingly implemented in forms, but it is inconvenient to use the verification codes written in js. so I learned how to implement php verification...