龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

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

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
自己写了一个验证码类,一起学习,希望有什么问题,一起交流,很喜欢php br /
自己写了一个验证码类,一起学习,希望有什么问题,一起交流,很喜欢php

<?php
// +------------------------------------------------------------------------
// 验证码类,该类的对象能动态获取验证码图片,验证码字符保存在SESSION['code']中 
// +------------------------------------------------------------------------
// 支持4种格式 数字 字母 汉字 混合
// +------------------------------------------------------------------------
// @Author: HelloChina(sanzi0930@163.com)                           
// +------------------------------------------------------------------------
// @Date: 2012年6月7日11:03:00				     
// +------------------------------------------------------------------------
// @version 1.0                                                     
// +------------------------------------------------------------------------

	class Vcode{
		protected $width;				//验证码宽度
		protected $height;				//验证码长度
		protected $codeNum;				//验证码字符个数
		protected $codeType;				//验证码类型
		protected $fontSize;				//字符大小
		protected $fontType;				//字体类型
		protected $codeStr;				//中文内容
		protected $strNum;				//中文个数
		protected $imageType;				//输出图片类型
		protected $image;				//图片资源
		protected $checkCode;				//验证码内容
     /**
      +--------------------------------------------------------------------------------
      * 取得验证码信息
      +--------------------------------------------------------------------------------
      * @param integer $width		验证码宽度 
      * @param integer $height		验证码高度 
      * @param integer $codeNum		验证码字符个数	
      * @param integer $codeType	验证码字符类型	1为数字 2为字母 3为汉字 4为混编
      * @param integer $fontSize        验证码字体的大小
      * @param string  $fontType	验证码字体类型
      * @param string  $imageType	验证码输出图片类型
      * @param string  $codestr		中文验证码内容
      +--------------------------------------------------------------------------------
     */
		public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType='heiti.ttf' ,$imageType='jpeg', $codeStr='去我饿人他一哦平啊是的飞个好就看了在想才吧你吗'){
			$this->width	 =  $width;
			$this->height 	 =  $height;
			$this->codeNum 	 =  $codeNum;
			$this->codeType	 =  $codeType;
			$this->fontSize  =  $fontSize;
			$this->fontType	 =  $fontType;
			$this->codeStr	 =  $codeStr;
			$this->strNum = strlen($this->codeStr)/3-1;
			$this->imageType =  $imageType;
			$this->checkCode =  $this->getCheckCode();
		}

      //+--------------------------------------------------------------------------------
      //* 生成验证码字符
      //+--------------------------------------------------------------------------------
      //* @return string
      //+--------------------------------------------------------------------------------
		public function __toString(){			
			$string = implode('', $this->getCheckCode());
			$_SESSION["code"]=$string;  //加到session中
			$this->getImage();              //输出验证码
			return '';
		}	
		protected function getCheckCode(){
			$string = array();				
			switch($this->codeType){
				case 1:
					//数字字符串
					$string =  array_rand(range(0,9), $this->codeNum);
					break;
				case 2:
					//大字母字符串
					$string = array_rand(array_flip(range('A', 'Z')), $this->codeNum);
					break;
				case 3:
					//汉字字符串
					
					for($i=0; $i<($this->codeNum); $i++){
						$start = mt_rand(0, $this->strNum);
						$string[$i]= self::msubstr($this->codeStr,$start);
					}
					break;
				case 4:
					//混合字符串
					for($i=0; $i<($this->codeNum); $i++){
						$rand=mt_rand(0,2);
						switch($rand){
							case 0:
								$ascii = mt_rand(48,57);
								$string[$i] = sprintf('%c',$ascii);
								break;

2. [图片] 混合.jpg    

3. [图片] 数字.jpg    

4. [图片] 中文.jpg    

5. [图片] 字母.jpg    

6. [文件] 验证码类.zip ~ 1006KB     下载(276)     [全屏预览]

精彩图集

赞助商链接