Skip to content

Commit

Permalink
HTTP_X_FORWARDED_HOST
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan committed Jul 18, 2020
1 parent f2c7fad commit 89df817
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Service/OAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';

Expand All @@ -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');
}
Expand Down Expand Up @@ -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'];
}

/**
Expand Down

0 comments on commit 89df817

Please sign in to comment.