From faad40e8265245ba9d0224a783a806587850df3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E6=B4=9B=E6=B4=9B?= <1031601644@qq.com> Date: Wed, 28 Aug 2024 22:39:00 +0800 Subject: [PATCH] =?UTF-8?q?Request=E5=AE=8C=E5=96=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=97=B6=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9C=9F=E5=AE=9E=E5=AE=A2=E6=88=B7=E7=AB=AFIP?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Request.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/think/Request.php b/src/think/Request.php index ef585ffcb3..3d741d1694 100644 --- a/src/think/Request.php +++ b/src/think/Request.php @@ -81,7 +81,7 @@ class Request implements ArrayAccess * 前端代理服务器真实IP头 * @var array */ - protected $proxyServerIpHeader = ['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP']; + protected $proxyServerIpHeader = []; /** * 请求类型 @@ -1590,6 +1590,32 @@ public function isPjax(bool $pjax = false): bool return $this->param($this->varPjax) ? true : $result; } + /** + * 设置代理服务器信息 + * @param array $proxyServerIpHeader 代理服务器发来的真实IP的协议头。默认['X-REAL-IP','X-FORWARDED-FOR','CLIENT-IP','X-CLIENT-IP','X-CLUSTER-CLIENT-IP'] + * @param array $proxyServerIp 代理服务器IP数组,兼容CIDR格式。用于校验是否是真实的代理服务器,传入0.0.0.0/0表示不校验代理服务器IP + * @return $this + * @throws Exception 传入空数组时将抛出异常,因为你啥都不传你还用调用这个方法干嘛 + */ + public function setProxyServer(array $proxyServerIpHeader = [ + 'X-REAL-IP', + 'X-FORWARDED-FOR', + 'CLIENT-IP', + 'X-CLIENT-IP', + 'X-CLUSTER-CLIENT-IP' + ], array $proxyServerIp = ['0.0.0.0/0']) + { + if ($proxyServerIp == [] || $proxyServerIpHeader == []) { + throw new Exception('不可传入空数组'); + } + foreach ($proxyServerIp as &$value) { + //将$value加上HTTP_前缀,并将-替换为_ + $value = 'HTTP_' . str_replace('-', '_', $value); + } + $this->proxyServerIp = $proxyServerIp; + $this->proxyServerIpHeader = $proxyServerIpHeader; + return $this; + } /** * 获取客户端IP地址 * @access public