Message: Undefined index: REMOTE_HOST in $_SERVER
This is not an error, it's a notice. REMOTE_HOST is not defined in all cases. REMOTE_ADDR is. You need to reconfigure your webserver if you need it. HostnameLookups On
does it, but it incurs a slowdown.
Alternative: Let PHP do the lookup, so you can skip it (for speed) when not needed:
$r = $_SERVER["REMOTE_HOST"] ?: gethostbyaddr($_SERVER["REMOTE_ADDR"]);
The PHP manual for REMOTE_HOST
in $_SERVER
says:
Your web server must be configured to create this variable. For example in Apache you'll need HostnameLookups On inside httpd.conf for it to exist.
$r = $_SERVER["REMOTE_HOST"] ?: gethostbyaddr( $_SERVER["REMOTE_ADDR"]); // Will still cause the error/notice message
In order to avoid the message one should use:
$r = array_key_exists( 'REMOTE_HOST', $_SERVER) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr( $_SERVER["REMOTE_ADDR"]);
Or the simpler:
$r = is_set( $_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr( $_SERVER["REMOTE_ADDR"]);
Or as from PHP 7 the simplest:
$r = $_SERVER['REMOTE_HOST'] ?? gethostbyaddr( $_SERVER["REMOTE_ADDR"]);
And this is why I love PHP !