From 89df81724c5d21908e89a6f121a71647f821eb70 Mon Sep 17 00:00:00 2001 From: Ethan <190196539@qq.com> Date: Sat, 18 Jul 2020 18:11:21 +0800 Subject: [PATCH] HTTP_X_FORWARDED_HOST --- src/Service/OAuthService.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Service/OAuthService.php b/src/Service/OAuthService.php index d64166b..120e323 100755 --- a/src/Service/OAuthService.php +++ b/src/Service/OAuthService.php @@ -3,7 +3,6 @@ namespace PFinal\Wechat\Service; use PFinal\Wechat\Kernel; -use PFinal\Wechat\Support\Session; use PFinal\Wechat\WechatException; class OAuthService extends BaseService @@ -32,7 +31,7 @@ public static function getOpenid() * @return array * @throws WechatException */ - public static function getUser($openidOnly = false, $uri = null) + public static function getUser($openidOnly = false) { $state = 'PFINAL_WECHAT'; @@ -55,12 +54,16 @@ public static function getUser($openidOnly = false, $uri = null) $proto = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; } - //当前url - if ($uri == null) { - $uri = $proto . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; - $uri = static::urlClean($uri); + if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { + $host = $_SERVER['HTTP_X_FORWARDED_HOST']; + } else { + $host = $_SERVER['HTTP_HOST']; } + //当前url + $uri = $proto . '://' . $host . $_SERVER['REQUEST_URI']; + $uri = static::urlClean($uri); + //跳转到微信oAuth授权页面 self::redirect($uri, $state, $openidOnly ? 'snsapi_base' : 'snsapi_userinfo'); } @@ -139,7 +142,10 @@ private static function urlClean($uri, $remove = ['state', 'code']) $arr['path'] = array_key_exists('path', $arr) ? $arr['path'] : ''; $arr['query'] = array_key_exists('query', $arr) ? ('?' . $arr['query']) : ''; - return $arr['scheme'] . '://' . $arr['host'] . $arr['path'] . $arr['query']; + + $port = empty($arr['port']) ? '' : (':' . $arr['port']); + + return $arr['scheme'] . '://' . $arr['host'] . $port . $arr['path'] . $arr['query']; } /**