PHP code 驗(yàn)證碼生成類定義和簡單使用示例
本文實(shí)例講述了PHP code 驗(yàn)證碼生成類定義和簡單使用。分享給大家供大家參考,具體如下:
code.php
<?phpnamespace code;/** * Class Code */class Code{ protected $number;//驗(yàn)證碼內(nèi)字符個(gè)數(shù) protected $codeType;//驗(yàn)證碼樣式 protected $width;//圖像寬 protected $height;//圖像高 protected $code;//驗(yàn)證碼 protected $image;//圖像資源 /** * Code constructor. * @param int $number * @param int $codeType * @param int $width * @param int $height */ public function __construct($number=5, $codeType=2, $width=100, $height=40) { $this->number = $number; $this->codeType = $codeType; $this->width = $width; $this->height = $height; $this->code = $this->createCode(); } /** * 銷毀資源 */ public function __destruct() { imagedestroy($this->image); } /** * 外部調(diào)用code時(shí)觸發(fā) * @param $name * @return bool */ public function __get($name) { if (’code’ == $name) { return $this->$name; } else { return false; } } /** * 生成code */ protected function createCode() { switch ($this->codeType) { case 0:$code = $this->getNum();break; case 1:$code = $this->getChar();break; case 2:$code = $this->getNumChar();break; default:die(’樣式不對(duì)’); } return $code; } /** * 數(shù)字驗(yàn)證碼 * @return string */ protected function getNum() { $str = join(’’, range(0,9)); return substr(str_shuffle($str), 0, $this->number); } /** * 字符驗(yàn)證碼 * @return string */ protected function getChar() { $str = join(’’, range(’a’, ’z’)); $str = $str . strtoupper($str); return substr(str_shuffle($str), 0, $this->number); } /** * 字符和數(shù)字混合驗(yàn)證碼 * @return string */ protected function getNumChar() { $num = join(’’, range(0, 9)); $str = join(’’, range(’a’, ’z’)); $str_big = strtoupper($str); $numChar = $num . $str . $str_big; return substr(str_shuffle($numChar), 0, $this->number); } /** * 生成圖像 */ protected function createImage() { $this->image = imagecreatetruecolor($this->width, $this->height); } /** * 填充背景色 */ protected function fillColor() { imagefill($this->image, 0, 0, $this->lightColor()); } /** * 淺顏色 * @return int */ protected function lightColor() { return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255)); } /** * 深顏色 * @return int */ protected function darkColor() { return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); } /** * 添加驗(yàn)證碼字符 */ protected function drawChar() { $width = ceil($this->width/$this->number); for ($i = 0; $i < $this->number; $i++) { $x = mt_rand($i * ($width - 5), ($i + 1) * ($width - 5)); $y = mt_rand(0, $this->height - 15); imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor()); } } /** * 添加干擾點(diǎn) */ protected function drawDisturb() { for ($i= 0; $i < 100; $i++) { imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor()); } } /** * 添加干擾線 */ protected function drawArc() { for ($i = 0; $i < $this->number - 3; $i++) { imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkColor()); } } /** * 輸出顯示 */ protected function show() { header(’Content-Type:image/png’); imagepng($this->image); } /** * 外部image */ public function outImage() { $this->createImage();//創(chuàng)建畫布 $this->fillColor();//填充背景色 $this->drawChar();//添加驗(yàn)證字符 $this->drawDisturb();//添加干擾點(diǎn) $this->drawArc();//添加干擾線 $this->show();//輸出 }}
展示驗(yàn)證碼。。保存驗(yàn)證碼和過期時(shí)間
<?phpinclude ’./code/Code.php’; $code = new codeCode();$code->outImage();session_start();$_SESSION[’code’] = [ ’code’ => $code->code, ’exp_time’ => time() + (60 * 60 * 10),];
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. php模擬實(shí)現(xiàn)斗地主發(fā)牌2. Python random庫使用方法及異常處理方案3. 理解PHP5中static和const關(guān)鍵字4. spring acegi security 1.0.0 發(fā)布5. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)6. Vuex localStorage的具體使用7. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼8. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟9. vue 使用localstorage實(shí)現(xiàn)面包屑的操作10. MyBatis中的JdbcType映射使用詳解

網(wǎng)公網(wǎng)安備