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 ];