驗證碼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;</pre>
本文由用戶 cme7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!