PHP產生圖形驗證碼(加強幹擾型)

安安杰尼
發布: 2023-04-08 15:16:01
原創
195162 人瀏覽過

驗證碼使用場景

我們在開發系統的過程中,基本上所有的系統都會涉及登入模組,其中驗證碼功能是這裡面必不可少的一塊,是防止系統被爆破的有效途徑。所謂道高一尺魔高一丈,現在的驗證碼越來越複雜先進,常見的字母數字驗證碼,行為驗證碼。本文詳細介紹簡單的字母數字驗證碼。

程式碼

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();
登入後複製

這樣即可輸出如下驗證碼

PHP產生圖形驗證碼(加強幹擾型)

##可調整參數控制驗證碼的大小,幹擾項等。

拓展


接下來介紹下拓展的功能,怎麼加強驗證碼的干擾項,怎麼結合到專案麗進行登入驗證。

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. 存取專案驗證

新文件,程式碼如下

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產生圖形驗證碼(加強幹擾型)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!