Verification code PHP class supports numbers, letters, Chinese characters, mixed

WBOY
Release: 2016-07-25 08:46:21
Original
827 people have browsed it
  1. // +---------------------------------------- ----------------------------------
  2. // Verification code class, objects of this class can dynamically obtain verification codes Pictures and verification code characters are saved in SESSION['code']
  3. // +-------------------------------- ----------------------------------------
  4. //Supports 4 formats of numbers, letters and Chinese characters Mix
  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; //Verification code number of 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; //Chinese number
  21. protected $imageType; //Output image type
  22. protected $image; //Image resource
  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 Verification code character Number
  31. * @param integer $codeType Verification code character type 1 is number 2 is letter 3 is Chinese character 4 is mixed
  32. * @param integer $fontSize Verification code font size
  33. * @param string $fontType Verification code font type
  34. * @param string $imageType Verification code output image 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='Fuck me, it's flat. Yes, it flies well. Just think about it.'){
  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 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. //Chinese character 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 strings
  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 Xiaobei on 2015-11-18 08:23


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template