PHP はグラフィック検証コードを生成します (拡張干渉タイプ)

安安杰尼
リリース: 2023-04-08 15:16:01
オリジナル
195348 人が閲覧しました

認証コードの利用シナリオ

システム開発のプロセスでは、基本的にすべてのシステムにログインモジュールが含まれますが、その認証コード機能はシステムの爆発を防ぐために不可欠な部分です。 。ことわざにあるように、悪魔は道のように高い 現在、認証コードはますます複雑かつ高度になり、一般的な英数字認証コードと動作認証コードが一般的になっています。この記事では、単純な英数字の確認コードについて詳しく説明します。

コード

<?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 = &#39;/outputs/font/font.ttf&#39;;
    }

    //创建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[&#39;VerifyCode&#39;] = strtolower($this->code);
        Session::set(&#39;VerifyCode&#39;, 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), &#39;*&#39;, $color);
        }
    }

    //输出背景
    private function outPut()
    {
        //生成标头
        header(&#39;Content-type:image/png&#39;);
        //输出图片
        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(&#39;VerifyCode&#39;) == strtolower($code)) {
            if($clear) $this->clearCode();
            return true;
        }
        if($clear) $this->clearCode();
        return false;
    }

    //清除验证码
    public function clearCode()
    {
        Session::del(&#39;VerifyCode&#39;);
//        session_start();
//        unset ($_SESSION[&#39;VerifyCode&#39;]);
    }
}
ログイン後にコピー

検証

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), &#39;*&#39;, $color);
    }
}
ログイン後にコピー

の上の数字 6 を変更します。ゆっくり調整できます。満足するまで効果を確認してください。同時に、検証コードには雪の結晶のエフェクトが多数含まれていることがわかります。これも干渉アイテムです。上記の数値 45 を修正して満足のいく結果に調整できます。

注: コード内の $charset 変数。小文字の i と L の効果を区別するのが難しいため、検証コードは検証に耐えるためにここから文字をランダムに選択します。そこで、i 文字を削除しました。

2. プロジェクトの検証にアクセスします

次のコードで新しいファイルを作成します

ログイン後にコピー

次に、これを既存のシステムのログイン ページに導入します。インターフェースは検証コードを表示でき、ユーザーが入力して送信すると、サーバーは次の検証を実行します

//验证验证码
public function checkCode($code, $clear = false)
{
    if (Session::get(&#39;VerifyCode&#39;) == strtolower($code)) {
        if($clear) $this->clearCode();
            return true;
    }
    if($clear) $this->clearCode();
    return false;
}
//清除验证码
public function clearCode()
{
    Session::del(&#39;VerifyCode&#39;);
}
ログイン後にコピー

この時点で、検証コードの生成と検証プロセスは完了です。

以上がPHP はグラフィック検証コードを生成します (拡張干渉タイプ)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!