Returns the host name.
This method can read the client port from the "X-Forwarded-Host" header when trusted proxies were set via "setTrustedProxies()".
The "X-Forwarded-Host" header must contain the client host name.
If your reverse proxy uses a different header name than "X-Forwarded-Host", configure it via "setTrustedHeaderName()" with the "client-host" key.
@api
string
\UnexpectedValueException when the host name is invalid
public function getHost() {
if (self::$trustedProxies && self::$trustedHeaders[self::HEADER_CLIENT_HOST] && ($host = $this->headers
->get(self::$trustedHeaders[self::HEADER_CLIENT_HOST]))) {
$elements = explode(',', $host);
$host = $elements[count($elements) - 1];
}
elseif (!($host = $this->headers
->get('HOST'))) {
if (!($host = $this->server
->get('SERVER_NAME'))) {
$host = $this->server
->get('SERVER_ADDR', '');
}
}
// trim and remove port number from host
// host is lowercase as per RFC 952/2181
$host = strtolower(preg_replace('/:\\d+$/', '', trim($host)));
// as the host can come from the user (HTTP_HOST and depending on the configuration, SERVER_NAME too can come from the user)
// check that it does not contain forbidden characters (see RFC 952 and RFC 2181)
if ($host && !preg_match('/^\\[?(?:[a-zA-Z0-9-:\\]_]+\\.?)+$/', $host)) {
throw new \UnexpectedValueException('Invalid Host');
}
return $host;
}