PHP verification code file and call method code details,
The Code is as follows:
// Verification code class ValidateCode {private $ charset = 'abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyz23456789 '; // random factor private $ code; // Verification code private $ codelen = 4; // verification code length: private $ width = 130; // width: private $ height = 50; // height: private $ img; // graphic resource handle: private $ font; // specify the font private $ fontsize = 20; // specify the font size private $ fontcolor; // specify the font color // initialize public function _ construct () {$ this-> font = dirname (_ FILE __). '/font/elephant. ttf'; // note that the font path must be correct; otherwise, the image cannot be displayed} // generate the random code private function createCode () {$ _ len = strlen ($ this-> charset) -1; for ($ I = 0; $ I <$ this-> codelen; $ I ++) {$ this-> code. = $ this-> charset [mt_rand (0, $ _ len)] ;}// generate the background private function createBg () {$ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height); $ color = imagecolorallocate ($ this-> img, mt_rand (157,255 ), mt_rand (157,255), mt_rand (157,255); imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color );} // generate the text private function createFont () {$ _ x = $ this-> width/$ this-> codelen; for ($ I = 0; $ I <$ this-> codelen; $ I ++) {$ this-> fontcolor = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156 ), mt_rand (0,156); imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30,30), $ _ x * $ I + mt_rand ), $ this-> height/1.4, $ this-> fontcolor, $ this-> font, $ this-> code [$ I]);} // generate the line and snowflake private function createLine () {// line for ($ I = 0; $ I <6; $ I ++) {$ color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ), $ color) ;}// snowflake for ($ I = 0; $ I <100; $ I ++) {$ color = imagecolorallocate ($ this-> img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255); imagestring ($ this-> img, mt_rand (), mt_rand (0, $ this-> width ), mt_rand (0, $ this-> height), '*', $ color) ;}// outPut private function outPut () {header ('content-type: image/png '); imagepng ($ this-> img); imagedestroy ($ this-> img);} // generate public function doimg () {$ this-> createBg (); $ this-> createCode (); $ this-> createLine (); $ this-> createFont (); $ this-> outPut ();} // obtain the verification code public function getCode () {return strtolower ($ this-> code );}}
Usage:
1. Save the verification code class as a file named ValidateCode. class. php;
2. Create a file named captcha. php to call this class;
Captcha. php
3. reference the code to the page as follows:
4. complete verification page with the following code:
<? Phpsession_start (); // enable the session first on the top, // error_reporting (2047); session_destroy (); // remove the session to get the new session value every time; // The effect of using seesion is good and convenient?> <Html>
Summary
The above is a detailed explanation of the PHP verification code file and call Method Code introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!