Verification code PHP class support numbers, letters, kanji, mixed

Source: Internet
Author: User
  1. // +------------------------------------------------------------------------
  2. Verification code class, the object of this class can dynamically get verification code picture, verify code word characters in session[' code ')
  3. // +------------------------------------------------------------------------
  4. Supports 4-format alphanumeric character blending
  5. // +------------------------------------------------------------------------
  6. @Author: Hellochina (sanzi0930@163.com)
  7. // +------------------------------------------------------------------------
  8. @Date: June 7, 2012 11:03:00
  9. // +------------------------------------------------------------------------
  10. @version 1.0
  11. // +------------------------------------------------------------------------
  12. Class vcode{
  13. protected $width; Verification Code width
  14. protected $height; Verification Code length
  15. protected $codeNum; Number of verification code characters
  16. protected $codeType; Verification code type
  17. protected $fontSize; Character size
  18. protected $fontType; Font type
  19. protected $codeStr; Chinese content
  20. protected $strNum; Number of Chinese
  21. protected $imageType; Output Picture Type
  22. protected $image; Picture Resources
  23. protected $checkCode; Verification Code Content
  24. /**
  25. +--------------------------------------------------------------------------------
  26. * Get Verification Code information
  27. +--------------------------------------------------------------------------------
  28. * @param integer $width Verification code width
  29. * @param integer $height Verification Code height
  30. * @param integer $codeNum number of verification code characters
  31. * @param integer $codeType Verification code character Type 1 is the number 2 is the letter 3 is the Chinese character 4 is mixed
  32. * @param the size of the integer $fontSize captcha font
  33. * @param string $fontType captcha font type
  34. * @param string $imageType Verification code output picture type
  35. * @param string $codestr Chinese verification Code content
  36. +--------------------------------------------------------------------------------
  37. */
  38. Public function __construct ($width =100, $height =50, $codeNum =4, $codeType =4, $fontSize =12, $fontType = ' Heiti.ttf ', $ Imagetype= ' jpeg ', $codeStr = ' Go to me hungry man he's a oh flat ah yes fly a good look at the think just you? '}
  39. $this->width = $width;
  40. $this->height = $height;
  41. $this->codenum = $codeNum;
  42. $this->codetype = $codeType;
  43. $this->fontsize = $fontSize;
  44. $this->fonttype = $fontType;
  45. $this->codestr = $codeStr;
  46. $this->strnum = strlen ($this->codestr)/3-1;
  47. $this->imagetype = $imageType;
  48. $this->checkcode = $this->getcheckcode ();
  49. }
  50. //+--------------------------------------------------------------------------------
  51. * Generate Verification Code characters
  52. //+--------------------------------------------------------------------------------
  53. * @return String
  54. //+--------------------------------------------------------------------------------
  55. Public Function __tostring () {
  56. $string = Implode (' ', $this->getcheckcode ());
  57. $_session["code"]= $string; Add to the session
  58. $this->getimage (); Output Verification Code
  59. Return ';
  60. }
  61. protected function Getcheckcode () {
  62. $string = Array ();
  63. Switch ($this->codetype) {
  64. Case 1:
  65. Numeric string
  66. $string = Array_rand (range (0,9), $this->codenum);
  67. Break
  68. Case 2:
  69. Large Letter String
  70. $string = Array_rand (Array_flip (Range (' A ', ' Z ')), $this->codenum);
  71. Break
  72. Case 3:
  73. Kanji string
  74. for ($i =0; $i < ($this->codenum); $i + +) {
  75. $start = Mt_rand (0, $this->strnum);
  76. $string [$i]= self::msubstr ($this->codestr, $start);
  77. }
  78. Break
  79. Case 4:
  80. Mixed string
  81. for ($i =0; $i < ($this->codenum); $i + +) {
  82. $rand =mt_rand (0,2);
  83. Switch ($rand) {
  84. Case 0:
  85. $ascii = Mt_rand (48,57);
  86. $string [$i] = sprintf ('%c ', $ascii);
  87. Break
Copy Code
Verification Code, PHP This topic was moved by Beckham on 2015-11-18 08:23
  • Related Article

    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.