Why do I get this error when I try to retrieve host name of remote user ?
Message: Undefined index: REMOTE_HOST
When reading documentation I came to know that it needs to be enabled in httpd.conf. But I am not sure what needs to be edited in httpd.conf.
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 !
I have faced this problem when using PHPUnit. This is how I deal with:
  $_SERVER["REMOTE_ADDR"]     = array_key_exists( 'REMOTE_ADDR',      $_SERVER) ? $_SERVER['REMOTE_ADDR']     : '127.0.0.1'; 
  $_SERVER["REMOTE_HOST"]     = array_key_exists( 'REMOTE_HOST',      $_SERVER) ? $_SERVER['REMOTE_HOST']     : gethostbyaddr($_SERVER["REMOTE_ADDR"]);
  $_SERVER["SERVER_PROTOCOL"] = array_key_exists( 'SERVER_PROTOCOL',  $_SERVER) ? $_SERVER['SERVER_PROTOCOL'] : "HTTP/1.1";
  $_SERVER["REQUEST_METHOD"]  = array_key_exists( 'REQUEST_METHOD',   $_SERVER) ? $_SERVER['REQUEST_METHOD']  : "GET";
  $_SERVER["SERVER_PORT"]     = array_key_exists( 'SERVER_PORT',      $_SERVER) ? $_SERVER['SERVER_PORT']     : "80";
  $_SERVER["SERVER_SOFTWARE"] = array_key_exists( 'SERVER_SOFTWARE',  $_SERVER) ? $_SERVER['SERVER_SOFTWARE'] : "Apache";
  $_SERVER["HTTP_ACCEPT"]     = array_key_exists( 'HTTP_ACCEPT',      $_SERVER) ? $_SERVER['HTTP_ACCEPT']     : "text/html,application/xhtml+xml,application/xml,application/json";
  $_SERVER["HTTP_HOST"]       = array_key_exists( 'HTTP_HOST',        $_SERVER) ? $_SERVER['HTTP_HOST']       : "www.site.com";
  $_SERVER["HTTP_USER_AGENT"] = array_key_exists( 'HTTP_USER_AGENT',  $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36';
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With