A PHP verification code generated class code

Source: Internet
Author: User
  1. Class Validationcode
  2. {
  3. Property
  4. Private $width;
  5. Private $height;
  6. Private $codeNum;
  7. Private $image;
  8. Private $disturbColorNum; Number of interfering elements
  9. Private $checkCode;
  10. function __construct ($width =80, $height =20, $codeNum =4)
  11. {
  12. $this->width= $width;
  13. $this->height= $height;
  14. $this->codenum= $codeNum;
  15. $number =floor ($width * $height/15);
  16. if ($number >240-$codeNum)
  17. {
  18. $this->disturbcolornum=240-$codeNum;
  19. }else
  20. {
  21. $this->disturbcolornum= $number;
  22. }
  23. $this->checkcode= $this->createcheckcode ();
  24. }
  25. function Getcheckcode ()
  26. {
  27. return $this->checkcode;
  28. }
  29. Private Function CreateImage () {
  30. $this->image=imagecreatetruecolor ($this->width, $this->height);
  31. $backcolor =imagecolorallocate ($this->image,rand (225,255), Rand (225,255), Rand (255,255));
  32. Imagefill ($this->image,0,0, $backcolor);
  33. $border =imagecolorallocate ($this->image,0,0,0);
  34. Imagerectangle ($this->image,0,0, $this->width-1, $this->height-1, $border);
  35. }
  36. Private Function Setdisturbcolor () {
  37. for ($i =0; $i < $this->disturbcolornum; $i +) {
  38. $color =imagecolorallocate ($this->image,rand (0,255), Rand (0,255), Rand (0,255));
  39. Imagesetpixel ($this->image,rand (1, $this->width-2), rand (1, $this->height-2), $color);
  40. }
  41. for ($i =0; $i <10; $i + +)
  42. {
  43. $color =imagecolorallocate ($this->image,rand (0,255), Rand (0,255), Rand (0,255));
  44. Imagearc ($this->image,rand ( -10, $this->width), rand ( -10, $this->height), rand (30,300), Rand (20,300), 55, 44, $color);
  45. }
  46. }
  47. Private Function outputtext ($fontFace = "") {
  48. for ($i =0; $i < $this->codenum; $i + +)
  49. {
  50. $fontcolor =imagecolorallocate ($this->image,rand (0,128), Rand (0,128), Rand (0,128));
  51. if ($fontFace = = "")
  52. {
  53. $fontsize =rand (3,5);
  54. $x =floor ($this->width/$this->codenum) * $i +5;
  55. $y =rand (0, $this->height-15);
  56. Imagechar ($this->image, $fontsize, $x, $y, $this->checkcode{$i}, $fontcolor);
  57. }
  58. Else
  59. {
  60. $fontsize =rand (12,16);
  61. $x =floor (($this->width-8)/$this->codenum) * $i +8;
  62. $y =rand ($fontsize, $this->height-8);
  63. Imagettftext ($this->image, $fontsize, Rand ( -45,45), $x, $y, $fontcolor, $fontFace, $this->checkcode{$i});
  64. }
  65. }
  66. }
  67. Private Function Createcheckcode () {
  68. $code = "23456789abcdefghijkmnpqrstuvwrst";
  69. $str = "";
  70. for ($i =0; $i < $this->codenum; $i + +)
  71. {
  72. $char = $code {rand (0,strlen ($code)-1)};
  73. $str. = $char;
  74. }
  75. return $str;
  76. }
  77. Private Function Outputimage ()
  78. {
  79. if (Imagetypes () &img_gif)
  80. {
  81. Header ("Content-type:image/gif");
  82. Imagepng ($this->image);
  83. }else if (imagetypes () &img_jpg)
  84. {
  85. Header ("Content-type:image/jpeg");
  86. Imagepng ($this->image);
  87. }else if (imagetypes () &img_png)
  88. {
  89. Header ("Content-type:image/png");
  90. Imagepng ($this->image);
  91. }else if (imagetypes () &img_wbmp) {
  92. Header ("Content-type:image/vnd.wap.wbmp");
  93. Imagepng ($this->image);
  94. }else
  95. {
  96. Die ("PHP does not support image verification code");
  97. }
  98. }
  99. Use this method to output an image to the browser
  100. function ShowImage ($fontFace = "")
  101. {
  102. Create an image background
  103. $this->createimage ();
  104. Setting interference elements
  105. $this->setdisturbcolor ();
  106. Randomly draw text into an image
  107. $this->outputtext ($fontFace);
  108. Output image
  109. $this->outputimage ();
  110. }
  111. function __destruct ()
  112. {
  113. Imagedestroy ($this->image);
  114. }
  115. }
  116. function Checklogin () {
  117. if (Empty ($_post[' name '))
  118. Die (' User name cannot be empty ');
  119. if (Empty ($_post[' password '))
  120. Die ("Password cannot be empty");
  121. if ($_session[' code ']!=$_post[' vertify ')
  122. Die ("The captcha is not entered correctly". $_session[' code ');
  123. $username =$_post[' name '];
  124. $password =md5 ($_post[' password ');
  125. Check for Presence
  126. Conndb ($username, $password);
  127. }
  128. function conndb ($name = "", $ps = "") {
  129. $conn =mysql_connect (' localhost ', ' root ', ' 123456 ');
  130. if (! $conn) Die ("Database connection Failed". Mysql_error ());
  131. mysql_select_db (' 5kan ', $conn) or Die (' Select Database Failed '. Mysql_error ());
  132. Mysql_set_charset (' UTF8 ', $conn);
  133. $sql = "SELECT id from K_user where username= ' {$name} ' and password= ' {$ps} '";
  134. $result =mysql_query ($sql) or Die ("SQL statement error". Mysql_error ());
  135. if (mysql_num_rows ($result) >0) die ("Login Successful");
  136. Else die ("User name or password error");
  137. Mysql_close ($conn);
  138. }
  139. Session_Start ();
  140. if (!isset ($_post[' randnum '))
  141. {
  142. $code =new Validationcode (120,20,4);
  143. $code->showimage ("Comicbd.ttf"); Show on page
  144. $_session[' code ']= $code->getcheckcode ();//Save in Server
  145. }
  146. Else
  147. {
  148. Checklogin ();
  149. }
  150. ?>
Copy Code
Verification Code, PHP
  • 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.