Core\Request::host
Core\Request::host(): ?tainted stringReturns the host name the request was sent to, such as shop.example.com. The value comes from the Host header of the request.
The result is always in lower case. A port such as :8443 is removed, and so is one dot at the end of the name. So Shop.Example.com.:8443 returns shop.example.com. An IPv6 address keeps its brackets, for example [2001:db8::1]. The result is null when the request has no Host header.
The server answers 400 to an HTTP/1.1 request that has no Host header. It also answers 400 when the Host header has a character that is not ASCII, or when there are two Host headers. Your program does not run for these requests.
host reads only the Host header. Headers such as X-Forwarded-Host are never read, because any client can send them. If you need the port, Core\Request::header("host") returns the header exactly as it arrived.
The result is tainted, which means it came from outside your program. A command-line program answers no request, so host throws a LogicError there.
Return value
Returns ?tainted string. The host name as a tainted string in lower case. A port and one dot at the end are removed, so Shop.Example.com.:8443 returns shop.example.com. An IPv6 address keeps its brackets. The result is null when the request has no Host header. header("host") returns the header exactly as it arrived, with the port. Headers such as X-Forwarded-Host are never read.
Errors
LogicErrorThe program is not handling a request.
Examples
Reading the host name
<?nvs// Shows how to read the host name that a request was sent to.
// `Core\Request::host` returns the name in lower case, without the port.// This prints "shop.example.com".echo Core\Request::host() ?? 'none', "\n";
// `Core\Request::header` returns the `Host` header as it arrived.// This prints "Shop.Example.com:8443".echo Core\Request::header('host') ?? 'none', "\n";shop.example.comShop.Example.com:8443A request with no host
<?nvs// Shows what `Core\Request::host` returns when a request has no `Host` header.
// The request below has no `Host` header, so the result is `null`.?tainted string $host = Core\Request::host();
// Check for `null` before you use the value. This prints "400: the request has no host".if ($host == null) { echo "400: the request has no host\n";} else { echo 'the host is ', $host, "\n";}400: the request has no hostChoosing a shop by host name
<?nvs// Shows how one server runs several shops, and picks the shop by the host name of the request.
// Each host name belongs to one shop.array<string> $shops = [ 'shoes.example.com' => 'Shoe Shop', 'books.example.com' => 'Book Shop',];
// The request was sent to "Books.Example.com:8080". `Core\Request::host` returns// "books.example.com", so the lookup finds the shop.string $shop = $shops[Core\Request::host() ?? ''] ?? '';
// This prints "Welcome to Book Shop".if ($shop == '') { echo "404: no shop has this host name\n";} else { echo 'Welcome to ', $shop, "\n";}Welcome to Book Shop