PHP verification code and php verification code. PHP verification code, php verification code design a verification code class, you can call the verification code class at any time, save as ValidateCode. class. php1? Php2 verification code Class 3 session PHP verification code, php verification code
Design a verification code class that can be called at any time as needed
Verification code class, saved as ValidateCode. class. php
1
Font = '. /latha. ttf'; // note that the font path must be correct; otherwise, the image cannot be displayed 17} 18 // Generate random code 19 private function createCode () {20 $ _ len = strlen ($ this-> charset)-1; 21 for ($ I = 0; $ I <$ this-> codelen; $ I ++) {22 $ this-> code. = $ this-> charset [mt_rand (0, $ _ len)]; 23} 24} 25 // Generate background 26 private function createBg () {27 $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height); 28 $ color = imagecolorallocate ($ this-> img, mt_rand (157,255 ), mt_rand (157,255), mt_rand (157,255); 29 imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color ); 30} 31 // Generate the text 32 private function createFont () {33 $ _ x = $ this-> width/$ this-> codelen; 34 for ($ I = 0; $ I <$ this-> codelen; $ I ++) {35 $ this-> fontcolor = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156 ), mt_rand (0,156); 36 imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30,30), $ _ x * $ I + mt_rand ), $ this-> height/1.4, $ this-> fontcolor, $ this-> font, $ this-> code [$ I]); 37} 38} 39 // Generate line, snowflake 40 private function createLine () {41 // Line 42 for ($ I = 0; $ I <6; $ I ++) {43 $ color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); 44 imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color); 45} 46 // snowflake 47 for ($ I = 0; $ I <100; $ I ++) {48 $ color = imagecolorallocate ($ this-> img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255); 49 imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), '*', $ color ); 50} 51} 52 // outPut 53 private function outPut () {54 header ('content-type: image/png '); 55 imagepng ($ this-> img ); 56 imagedestroy ($ this-> img); 57} 58 // Generate 59 public function doimg () {60 $ this-> createBg (); 61 $ this-> createCode (); 62 $ this-> createLine (); 63 $ this-> createFont (); 64 $ this-> outPut (); 65} 66 // Get the verification code 67 public function getCode () {68 return strtolower ($ this-> code); 69} 70}
Note: In row 16th, you must modify the font path. Otherwise, the font image cannot be displayed.
Implementation, save as captcha. php
1 session_start (); 2 require './ValidateCode. class. php'; // first include the class, and modify the actual path according to the actual situation. 3 $ _ vc = new ValidateCode (); // instantiate an object 4 $ _ vc-> doimg (); 5 $ _ SESSION ['authnum _ session'] = $ _ vc-> getCode (); // the verification code is saved to the session.
Page usage
Reposted from: a beautiful php verification code class (share)
Pipeline designs a verification code class. when needed, it can call the verification code class at any time and save it as ValidateCode. class. php 1? Php 2 // verification code Class 3 session...