yii\web\Request::getHostInfo PHP Method

getHostInfo() public method

The returned URL does not have an ending slash. By default this value is based on the user request information. This method will return the value of $_SERVER['HTTP_HOST'] if it is available or $_SERVER['SERVER_NAME'] if not. You may want to check out the PHP documentation for more information on these variables. You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. > Warning: Dependent on the server configuration this information may not be > reliable and may be faked by the user sending the HTTP request. > If the webserver is configured to serve the same site independent of the value of > the Host header, this value is not reliable. In such situations you should either > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. > If you don't have access to the server configuration, you can setup HostControl filter at > application level in order to protect against such kind of attack.
See also: setHostInfo()
public getHostInfo ( ) : string | null
return string | null schema and hostname part (with port number if needed) of the request URL (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set.
    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;
    }

Usage Example

 /**
  * Parses the given request and returns the corresponding route and parameters.
  * @param UrlManager $manager the URL manager
  * @param Request $request the request component
  * @return array|boolean the parsing result. The route and the parameters are returned as an array.
  * If false, it means this rule cannot be used to parse this path info.
  */
 public function parseRequest($manager, $request)
 {
     if ($this->mode === self::CREATION_ONLY) {
         return false;
     }
     if (!empty($this->verb) && !in_array($request->getMethod(), $this->verb, true)) {
         return false;
     }
     $pathInfo = $request->getPathInfo();
     $suffix = (string) ($this->suffix === null ? $manager->suffix : $this->suffix);
     if ($suffix !== '' && $pathInfo !== '') {
         $n = strlen($suffix);
         if (substr_compare($pathInfo, $suffix, -$n) === 0) {
             $pathInfo = substr($pathInfo, 0, -$n);
             if ($pathInfo === '') {
                 // suffix alone is not allowed
                 return false;
             }
         } else {
             return false;
         }
     }
     if ($this->host !== null) {
         $pathInfo = strtolower($request->getHostInfo()) . ($pathInfo === '' ? '' : '/' . $pathInfo);
     }
     if (!preg_match($this->pattern, $pathInfo, $matches)) {
         return false;
     }
     foreach ($this->defaults as $name => $value) {
         if (!isset($matches[$name]) || $matches[$name] === '') {
             $matches[$name] = $value;
         }
     }
     $params = $this->defaults;
     $tr = [];
     foreach ($matches as $name => $value) {
         if (isset($this->_routeParams[$name])) {
             $tr[$this->_routeParams[$name]] = $value;
             unset($params[$name]);
         } elseif (isset($this->_paramRules[$name])) {
             $params[$name] = $value;
         }
     }
     if ($this->_routeRule !== null) {
         $route = strtr($this->route, $tr);
     } else {
         $route = $this->route;
     }
     Yii::trace("Request parsed with URL rule: {$this->name}", __METHOD__);
     return [$route, $params];
 }
All Usage Examples Of yii\web\Request::getHostInfo