Heim  >  Artikel  >  Backend-Entwicklung  >  验证码PHP类 支持数字,字母,汉字,混合

验证码PHP类 支持数字,字母,汉字,混合

WBOY
WBOYOriginal
2016-07-25 08:46:21789Durchsuche
  1. // +------------------------------------------------------------------------
  2. // 验证码类,该类的对象能动态获取验证码图片,验证码字符保存在SESSION['code']中
  3. // +------------------------------------------------------------------------
  4. // 支持4种格式 数字 字母 汉字 混合
  5. // +------------------------------------------------------------------------
  6. // @Author: HelloChina(sanzi0930@163.com)
  7. // +------------------------------------------------------------------------
  8. // @Date: 2012年6月7日11:03:00
  9. // +------------------------------------------------------------------------
  10. // @version 1.0
  11. // +------------------------------------------------------------------------
  12. class Vcode{
  13. protected $width; //验证码宽度
  14. protected $height; //验证码长度
  15. protected $codeNum; //验证码字符个数
  16. protected $codeType; //验证码类型
  17. protected $fontSize; //字符大小
  18. protected $fontType; //字体类型
  19. protected $codeStr; //中文内容
  20. protected $strNum; //中文个数
  21. protected $imageType; //输出图片类型
  22. protected $image; //图片资源
  23. protected $checkCode; //验证码内容
  24. /**
  25. +--------------------------------------------------------------------------------
  26. * 取得验证码信息
  27. +--------------------------------------------------------------------------------
  28. * @param integer $width 验证码宽度
  29. * @param integer $height 验证码高度
  30. * @param integer $codeNum 验证码字符个数
  31. * @param integer $codeType 验证码字符类型 1为数字 2为字母 3为汉字 4为混编
  32. * @param integer $fontSize 验证码字体的大小
  33. * @param string $fontType 验证码字体类型
  34. * @param string $imageType 验证码输出图片类型
  35. * @param string $codestr 中文验证码内容
  36. +--------------------------------------------------------------------------------
  37. */
  38. public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType='heiti.ttf' ,$imageType='jpeg', $codeStr='去我饿人他一哦平啊是的飞个好就看了在想才吧你吗'){
  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. //* 生成验证码字符
  52. //+--------------------------------------------------------------------------------
  53. //* @return string
  54. //+--------------------------------------------------------------------------------
  55. public function __toString(){
  56. $string = implode('', $this->getCheckCode());
  57. $_SESSION["code"]=$string; //加到session中
  58. $this->getImage(); //输出验证码
  59. return '';
  60. }
  61. protected function getCheckCode(){
  62. $string = array();
  63. switch($this->codeType){
  64. case 1:
  65. //数字字符串
  66. $string = array_rand(range(0,9), $this->codeNum);
  67. break;
  68. case 2:
  69. //大字母字符串
  70. $string = array_rand(array_flip(range('A', 'Z')), $this->codeNum);
  71. break;
  72. case 3:
  73. //汉字字符串
  74. for($i=0; $icodeNum); $i++){
  75. $start = mt_rand(0, $this->strNum);
  76. $string[$i]= self::msubstr($this->codeStr,$start);
  77. }
  78. break;
  79. case 4:
  80. //混合字符串
  81. for($i=0; $icodeNum); $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;
复制代码

验证码, PHP
本主题由 小贝 于 2015-11-18 08:23 移动


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn