重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
我們?cè)陂_發(fā)系統(tǒng)的過(guò)程中,基本所有的系統(tǒng)都會(huì)涉及到登錄模塊,其中驗(yàn)證碼功能是這里面必不可少的一塊,是防止系統(tǒng)被爆破的有效途徑。所謂道高一尺魔高一丈,現(xiàn)在的驗(yàn)證碼越來(lái)越復(fù)雜先進(jìn),常見的字母數(shù)字驗(yàn)證碼,行為驗(yàn)證碼。本文詳細(xì)介紹簡(jiǎn)單的字母數(shù)字驗(yàn)證碼。
創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元朔城做網(wǎng)站,已為上家服務(wù),為朔城各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
代碼
font = '/outputs/font/font.ttf'; } //創(chuàng)建4個(gè)隨機(jī)碼 private function createCode() { $_leng = strlen($this->charset) - 1; for ($i = 1; $i <= $this->codelen; $i++) { $this->code .= $this->charset[mt_rand(0, $_leng)]; } // session_start(); // $_SESSION['VerifyCode'] = strtolower($this->code); Session::set('VerifyCode', strtolower($this->code)); return $this->code; } //創(chuàng)建背景 private function createBg() { //創(chuàng)建畫布 給一個(gè)資源jubing $this->img = imagecreatetruecolor($this->width, $this->height); //背景顏色 $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); //畫出一個(gè)矩形 imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color); } //創(chuàng)建字體 private function createFont() { $_x = ($this->width / $this->codelen); //字體長(zhǎng)度 for ($i = 0; $i < $this->codelen; $i++) { //文字顏色 $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //資源句柄 字體大小 傾斜度 字體長(zhǎng)度 字體高度 字體顏色 字體 具體文本 imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $color, $this->font, $this->code[$i]); } } //隨機(jī)線條 private function createLine() { //隨機(jī)線條 for ($i = 0; $i < 6; $i++) { $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); } //隨機(jī)雪花 for ($i = 0; $i < 45; $i++) { $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } } //輸出背景 private function outPut() { //生成標(biāo)頭 header('Content-type:image/png'); //輸出圖片 imagepng($this->img); //銷毀結(jié)果集 imagedestroy($this->img); } //對(duì)外輸出 public function doimg() { //加載背景 $this->createBg(); //加載文件 $this->createCode(); //加載線條 $this->createLine(); //加載字體 $this->createFont(); //加載背景 $this->outPut(); } //獲取驗(yàn)證碼 public function getCode() { return strtolower($this->code); } //驗(yàn)證驗(yàn)證碼 public function checkCode($code, $clear = false) { // session_start(); if (Session::get('VerifyCode') == strtolower($code)) { if($clear) $this->clearCode(); return true; } if($clear) $this->clearCode(); return false; } //清除驗(yàn)證碼 public function clearCode() { Session::del('VerifyCode'); // session_start(); // unset ($_SESSION['VerifyCode']); } }
ob_clean(); $verify = new Code(); $verify->doimg();
這樣即可輸出如下驗(yàn)證碼
可以調(diào)整參數(shù)控制驗(yàn)證碼的大小,干擾項(xiàng)等。
接下來(lái)介紹下拓展的功能,怎么加強(qiáng)驗(yàn)證碼的干擾項(xiàng),怎么結(jié)合到項(xiàng)目麗進(jìn)行登錄驗(yàn)證。
1. 加強(qiáng)干擾
首先我們可以看到上面的截圖中少數(shù)線條,如果外者使用分析工具來(lái)解碼,那么會(huì)很簡(jiǎn)單的就解出我們的驗(yàn)證碼,這時(shí)候就需要添加線條的數(shù)量,在代碼中找到以下代碼并修改
//隨機(jī)線條 private function createLine() { //隨機(jī)線條 for ($i = 0; $i < 6; $i++) { $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); } //隨機(jī)雪花 for ($i = 0; $i < 45; $i++) { $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } }
上面的數(shù)字6可以慢慢調(diào)整,然后查看效果直到滿意。同時(shí)可以看到驗(yàn)證碼中有很多雪花效果,這個(gè)也是干擾項(xiàng),可以修改上面的數(shù)字45來(lái)調(diào)整到自己滿意的結(jié)果。
注意:代碼中的$charset變量,驗(yàn)證碼是從這邊隨機(jī)取字符來(lái)生存驗(yàn)證,由于小寫的i和L展示的效果很難分辨,所以我們?nèi)コ薸字符。
2. 接入項(xiàng)目驗(yàn)證
新建個(gè)文件,代碼如下
doimg();
然后在現(xiàn)有的系統(tǒng)登錄頁(yè)面引入這個(gè)接口即可展示驗(yàn)證碼,在用戶填寫提交之后,服務(wù)端做以下驗(yàn)證
//驗(yàn)證驗(yàn)證碼 public function checkCode($code, $clear = false) { if (Session::get('VerifyCode') == strtolower($code)) { if($clear) $this->clearCode(); return true; } if($clear) $this->clearCode(); return false; } //清除驗(yàn)證碼 public function clearCode() { Session::del('VerifyCode'); }
以上就是PHP生成圖形驗(yàn)證碼(加強(qiáng)干擾型)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!