Php verification code generator and Verification Code Generator
Many websites now have user sets. However, to prevent cyber attacks by robots. It is necessary to restrict login or registration.
It is a good choice to forcibly enter a string set that is hard to recognize on the machine during registration and login. Although they cannot solve the root problem, they can at least increase their costs.
The GD2 library is required to generate a verification code using PHP. There are many reference methods for the GD2 library on the network, and the import methods for different operating systems are also different.
This code runs on the ipvs Server Platform
<? Php $ iC = new idCode (5, 60, 30); $ iC-> createPNG (); class idCode {private $ words = array ('A', 'B ', 'C', 'D', 'E', 'F', 'G', 'h', 'I', 'J', 'k', 'l ', 'M', 'n', 'O', 'P', 'Q', 'R', 's', 't', 'U', 'V ', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F ', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P ', 'Q', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y', 'z ', '0', '1', '2', '3', '4', '5', '6', '7', '8 ', '9'); private $ fonts; private $ count; // number of verification code characters private $ height; priva Te $ width; private $ path = '.. \ myfolder \ fonts '; private $ keys; // constructor public function _ construct ($ count, $ width, $ height) {$ this-> count = $ count; $ this-> getFonts (); $ this-> height = $ height; $ this-> width = $ width;} private function getFonts () {$ dir = dir ($ this-> path); while (false! ==( $ File = $ dir-> read () {if ($ file! = '.' & $ File! = '.. ') {$ This-> fonts [count ($ this-> fonts)] = basename ($ file) ;}$ dir-> close ();} private function createKeys () {for ($ I = 0; $ I <$ this-> count; $ I ++) {$ this-> keys [$ I] ['Char '] = $ this-> words [rand (0, count ($ this-> words)-1)]; // use the font path to identify $ this-> keys [$ I] ['filename'] = $ this-> path. '\\'. $ this-> fonts [rand (0, count ($ this-> fonts)-1)] ;}} public function createPNG () {$ this-> createKeys (); // create a canvas and color block $ bg = imag Ecreatetruecolor ($ this-> width + 10*2, $ this-> height + 3*2); // leave 10px blank on both sides, up and down 3px $ gray = imagecolorallocate ($ bg, 155,155,155); $ blue = imagecolorallocate ($ bg, 0 x, 0x00, 0xff); // fill the background imagefill ($ bg, $ gray ); // Add the character $ pwidth = $ this-> width/$ this-> count; $ x; $ y; for ($ I = 0; $ I <$ this-> count; $ I ++) {$ rotation = rand (-40, 40); // deflection angle ± 40 ° $ fontsize = 33; $ width_txt; $ height_txt; do {$ fontsize --; $ bbox = imagettfb Box ($ fontsize, $ rotation, $ this-> keys [$ I] ['filename'], $ this-> keys [$ I] ['Char ']); $ width_txt = $ bbox [2]-$ bbox [0]; // x 0 2 4 6, y1 3 5 7; bottom left, bottom right, top right, top left $ height_txt = $ bbox [7]-$ bbox [1];} while ($ fontsize> 8 & ($ height_txt> $ this-> height | $ width_txt> $ pwidth); $ fontcolor = imagecolorallocate ($ bg, rand (0,255 ), rand (0,255), rand (0,255); $ x = 8 + $ pwidth * $ I + $ pwidth/2-$ width_txt/2; // x coordinate basic position $ y = $ this -> Height/2-$ height_txt/2; imagettftext ($ bg, $ fontsize, $ rotation, $ x, $ y, $ fontcolor, $ this-> keys [$ I] ['filename'], $ this-> keys [$ I] ['Char ']);} // draw the interference line // Add the interference line imageline ($ bg, $ blue) as needed based on the font; // the header file of the image output header ('content-type: image/png '); // output png image imagepng ($ bg); // clear cache resource imagedestroy ($ bg);} public function checkKeys ($ input) {if (count ($ input )! = $ This-> count) {return 'error: The length is incorrect. ';} else {for ($ I = 0; $ I <$ this-> count; $ I ++) {// 0 o I l 1 calibration, determine whether to manually calibrate if ($ input [$ I] based on the selected font! = $ This-> keys [$ I] ['Char ']) {return 'success. ';} else {return' ERROR: enter the correct verification code. ';}}}}?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.