A very practical php verification code class

Source: Internet
Author: User
The following small series will share with you a beautiful php verification code class. For more information, see the next practical php verification code class.


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 );}}

Output instance:

The code is as follows:

Session_start (); require './ValidateCode. class. php'; // first include the class, and modify the actual path according to the actual situation. $ _ Vc = new ValidateCode (); // instantiate an object $ _ vc-> doimg (); $ _ SESSION ['authnum _ session'] =$ _ vc-> getCode (); // the verification code is saved to the session.

3. reference the code to the page as follows:
The code is as follows:

 

4. complete verification page with the following code:

 Session image verification instance 
 ". $ _ SESSION [" authnum_session "]."
"; $ Validate =" "; if (isset ($ _ POST [" validate "]) {$ validate = $ _ POST [" validate "]; echo "you just entered :". $ _ POST ["validate"]."
Status: "; if ($ validate! = $ _ SESSION ["authnum_session"]) {// check whether the session value is consistent with the verification code entered by the user; echo "incorrect input ";} else {echo "verified" ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.