Thinkphp6筆記十六:IP黑名單


CheckIp.php代碼:文件位置app\middleware\CheckIp.php

<?php
namespace app\middleware;

class CheckIp
{
    // 狀態 關閉:false,開啟:true
    private $status = true;
    // ip庫
    private $StoreIp = ['127.0.0.2','127.0.0.1'];

    /**
     * 處理請求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 禁止訪問
        if($this->status==true){
            if(in_array($request->ip(),$this->StoreIp)){
                exit('禁止訪問');
            }
        }
        return $next($request);
    }


}

 

加載中間件:app/index/middleware.php

<?php
// 這是系統自動生成的middleware定義文件
return [
    'think\middleware\LoadLangPack',
     // 加載中間件
    app\middleware\CheckIp::class
];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM