Yii2: yii\web\Request::getHostInfo() has bug,cause project cannot run in 127.0.0.1:8080

Created on 20 Jan 2017  路  5Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

nginx listen port 8080 run
in chrome url address input 127.0.0.1:8080 to access yii2 project ,and login ,when login success will redirect to home page,now is error ,url is change to 127.0.0.1,lost the port 8080

What is the expected result?

response->redirect is not run

What do you get instead?

change function yii\web\Request::getHostInfo()

old function is :

public function getHostInfo()
{
    if ($this->_hostInfo === null) {
        $secure = $this->getIsSecureConnection();
        $http = $secure ? 'https' : 'http';
        if (isset($_SERVER['HTTP_HOST'])) {
            $this->_hostInfo = $http . '://' . $_SERVER['HTTP_HOST'];
        } elseif (isset($_SERVER['SERVER_NAME'])) {
            $this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME'];
            $port = $secure ? $this->getSecurePort() : $this->getPort();
            if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) {
                $this->_hostInfo .= ':' . $port;
            }
        }
    }
    return $this->_hostInfo;
}

instead function is :

public function getHostInfo()
{
    if ($this->_hostInfo === null) {
        $secure = $this->getIsSecureConnection();
        $http = $secure ? 'https' : 'http';
        if (isset($_SERVER['HTTP_HOST'])) {
            $this->_hostInfo = $http . '://' . $_SERVER['HTTP_HOST'];
        } elseif (isset($_SERVER['SERVER_NAME'])) {
            $this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME'];
        }
        $port = $secure ? $this->getSecurePort() : $this->getPort();
        if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) {
            $this->_hostInfo .= ':' . $port;
        }
    }
    return $this->_hostInfo;
}

Additional info

the instead code is just move code :

        $port = $secure ? $this->getSecurePort() : $this->getPort();
        if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) {
            $this->_hostInfo .= ':' . $port;
        }

out of "elseif (isset($_SERVER['SERVER_NAME'])) {"

| Q | A
| ---------------- | ---
| Yii version | 2.0.?
| PHP version |
| Operating system |

need more info

All 5 comments

Would you please share configuration files necessary to reproduce the issue using basic project template?

FWIW I have many projects that I develop on localhost on port 8080 and never had this issue. It must have to do with local configuration. Here's a log, notice the correct host + port in the Location: header.

$ curl -I http://127.0.0.1:8080/
HTTP/1.1 302 Found
Date: Fri, 20 Jan 2017 11:08:52 GMT
Server: Apache/2.4.10 (Debian) PHP/7.0.8
X-Powered-By: PHP/7.0.8
Set-Cookie: PHPSESSID=fc1737865e3e3095c36e4f3d3c433d4a; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Location: http://127.0.0.1:8080/site/login
Content-Type: text/html; charset=UTF-8

i say login ,and call yii\web\response::redirect function to redirect锛宎nd bug is appear,not "curl -I http://127.0.0.1:8080/ "

Well, maybe then really show some code. How do you redirect? Or do you talk about automatic redirect to returnUrl after login?

Having the app running on port 8080 is a very common scenario that many developers use. There never was any issue with this.

you are right,i find my nginx config set HTTP_HOST like fastcgi_param HTTP_HOST $http_host; so the HTTP_HOST is not have port, is my mistake

Was this page helpful?
0 / 5 - 0 ratings