IpCheck.php 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare (strict_types = 1);
  11. namespace app\common\middleware;
  12. use think\exception\HttpResponseException;
  13. use think\Response;
  14. class IpCheck
  15. {
  16. public function handle($request, \Closure $next)
  17. {
  18. $ip = $request->ip();
  19. $forbiddenip = site_config('basic.forbiddenip');
  20. if($forbiddenip){
  21. $forbiddenipArr=array_filter(explode("\n", str_replace("\r\n", "\n", $forbiddenip)));;
  22. if (count($forbiddenipArr)>0 && \Symfony\Component\HttpFoundation\IpUtils::checkIp($ip, $forbiddenipArr)) {
  23. $response = Response::create('请求无权访问', 'html', 403);
  24. throw new HttpResponseException($response);
  25. }
  26. }
  27. return $next($request);
  28. }
  29. }