可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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. Can anybody help me figuring out the directive?
Thanks in advance :)
回答1:
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"]);
回答2:
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.
回答3:
$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"]);
回答4:
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';
回答5:
Edit httpd.conf in your web server, add this line HostnameLookups On at the end of the file, save and restart your server.
回答6:
The best way:
$isp = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr($datosVista['ip']);