Url类的getCurrentUrl()方法判断HTTPS出错

bug问题 · hetao · 于 5年前 发布 · 2400 次阅读

文件 vendor\fancyecommerce\fecshop\services\Url.php 代码

    public function getCurrentUrl()
    {
        if (!$this->_currentUrl) {
            $this->_currentUrl = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        }
        
        return $this->_currentUrl;
    }

关于$_SERVER['HTTPS']的解释 $_SERVER['HTTPS'] — 如果通过https访问,则被设为一个非空的值(on),否则返回off

所以正确的写法应该是 isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on'

共收到 6 条回复
myred08#15年前 0 个赞

Yii2有一个函数可以判断

$secure = Yii::$app->getRequest()->getIsSecureConnection();
            $http = $secure ? 'https' : 'http';
    /**
     * Return if the request is sent via secure channel (https).
     * @return bool if the request is sent via secure channel (https)
     */
    public function getIsSecureConnection()
    {
        if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) {
            return true;
        }
        foreach ($this->secureProtocolHeaders as $header => $values) {
            if (($headerValue = $this->headers->get($header, null)) !== null) {
                foreach ($values as $value) {
                    if (strcasecmp($headerValue, $value) === 0) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

Terry#25年前 0 个赞

好的,多谢贡献。

myred08#35年前 0 个赞
$http_type = ((isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0)) ? 'https://' : 'http://'; 
Fecmall#45年前 0 个赞

直接用Yii2的吧

 public function getCurrentUrl()
    {
        if (!$this->_currentUrl) {
            $secure = Yii::$app->getRequest()->getIsSecureConnection();
            $http = $secure ? 'https' : 'http';
            $this->_currentUrl = $http . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        }
        
        return $this->_currentUrl;
    }
myred08#65年前 1 个赞

恩, OK。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics