我們在開發系統的過程中,基本所有的系統都會涉及到登錄模塊,其中驗證碼功能是這里面必不可少的一塊,是防止系統被爆破的有效途徑。所謂道高一尺魔高一丈,現在的驗證碼越來越復雜先進,常見的字母數字驗證碼,行為驗證碼。本文詳細介紹簡單的字母數字驗證碼。
代碼
<?php /********************************************************************************* * InitPHP 3.8.2 國產PHP開發框架 擴展類庫-驗證碼 *------------------------------------------------------------------------------- * 版權所有: CopyRight By initphp.com * 您可以自由使用該源碼,但是在使用過程中,請保留作者信息。尊重他人勞動成果就是尊重自己 *------------------------------------------------------------------------------- * Author:zhuli Dtime:2014-11-25 ***********************************************************************************/ class Code { private $charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; //隨機因子 private $code; //驗證碼文字 private $codelen = 4; //驗證碼顯示幾個文字 private $width = 100; //驗證碼寬度 private $height = 40; //驗證碼高度 private $img; //驗證碼資源句柄 private $font; //指定的字體 private $fontsize = 20; //指定的字體大小 private $fontcolor; //字體顏色 隨機 //構造類 編寫字體 public function __construct() { $this->font = '/outputs/font/font.ttf'; } //創建4個隨機碼 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; } //創建背景 private function createBg() { //創建畫布 給一個資源jubing $this->img = imagecreatetruecolor($this->width, $this->height); //背景顏色 $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); //畫出一個矩形 imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color); } //創建字體 private function createFont() { $_x = ($this->width / $this->codelen); //字體長度 for ($i = 0; $i < $this->codelen; $i++) { //文字顏色 $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //資源句柄 字體大小 傾斜度 字體長度 字體高度 字體顏色 字體 具體文本 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]); } } //隨機線條 private function createLine() { //隨機線條 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); } //隨機雪花 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() { //生成標頭 header('Content-type:image/png'); //輸出圖片 imagepng($this->img); //銷毀結果集 imagedestroy($this->img); } //對外輸出 public function doimg() { //加載背景 $this->createBg(); //加載文件 $this->createCode(); //加載線條 $this->createLine(); //加載字體 $this->createFont(); //加載背景 $this->outPut(); } //獲取驗證碼 public function getCode() { return strtolower($this->code); } //驗證驗證碼 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; } //清除驗證碼 public function clearCode() { Session::del('VerifyCode'); // session_start(); // unset ($_SESSION['VerifyCode']); } }
ob_clean(); $verify = new Code(); $verify->doimg();
這樣即可輸出如下驗證碼
可以調整參數控制驗證碼的大小,干擾項等。
接下來介紹下拓展的功能,怎么加強驗證碼的干擾項,怎么結合到項目麗進行登錄驗證。
1. 加強干擾
首先我們可以看到上面的截圖中少數線條,如果外者使用分析工具來解碼,那么會很簡單的就解出我們的驗證碼,這時候就需要添加線條的數量,在代碼中找到以下代碼并修改
//隨機線條 private function createLine() { //隨機線條 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); } //隨機雪花 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); } }
上面的數字6可以慢慢調整,然后查看效果直到滿意。同時可以看到驗證碼中有很多雪花效果,這個也是干擾項,可以修改上面的數字45來調整到自己滿意的結果。
注意:代碼中的$charset變量,驗證碼是從這邊隨機取字符來生存驗證,由于小寫的i和L展示的效果很難分辨,所以我們去除了i字符。
2. 接入項目驗證
新建個文件,代碼如下
<?php ob_clean(); $verify = new Code(); $verify->doimg();
然后在現有的系統登錄頁面引入這個接口即可展示驗證碼,在用戶填寫提交之后,服務端做以下驗證
//驗證驗證碼 public function checkCode($code, $clear = false) { if (Session::get('VerifyCode') == strtolower($code)) { if($clear) $this->clearCode(); return true; } if($clear) $this->clearCode(); return false; } //清除驗證碼 public function clearCode() { Session::del('VerifyCode'); }
以上就是PHP生成圖形驗證碼(加強干擾型)的詳細內容,更多請關注億速云其它相關文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。