PHP verification code and php Verification Code

Source: Internet
Author: User

PHP verification code and 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 <? Php 2 // Verification code class 3 session_start (); 4 class ValidateCode {5 private $ charset = 'abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyz23456789 '; // random factor 6 private $ code; // Verification Code 7 private $ codelen = 4; // verification code length 8 private $ width = 130; // width 9 private $ height = 50; // height 10 private $ img; // graphic resource handle 11 private $ font; // The specified font 12 private $ fontsize = 20; // specify the font size 13 private $ fontcolor; // specify the font color 14 // constructor initializes 15 public function _ construct () {16 $ this-> 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

 </img>

Reposted from: a beautiful php Verification Code Class (SHARE)

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.