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