Message: Undefined index: REMOTE_HOST in $ _SERVER

Why am I getting this error when trying to get the host name of a remote user?

Message: Undefined index: REMOTE_HOST 

When reading the documentation, I found out that it needs to be included in httpd.conf. But I'm not sure what to edit in httpd.conf. Can someone help me deal with the directive?

Thanks in advance:)

+6
php
source share
6 answers

This is not a mistake, this is a notification. REMOTE_HOST is not defined in all cases. REMOTE_ADDR is. You need to reconfigure your web server if you need it. HostnameLookups On does this, but it slows down.

Alternative: let PHP do a search, so you can skip it (for speed) when you don't need it:

 $r = $_SERVER["REMOTE_HOST"] ?: gethostbyaddr($_SERVER["REMOTE_ADDR"]); 
+14
source share

The PHP manual for REMOTE_HOST in $_SERVER says:

Your web server must be configured to create this variable. For example, in Apache you will need HostnameLookups On inside httpd.conf for it to exist.

+4
source share
 $r = $_SERVER["REMOTE_HOST"] ?: gethostbyaddr($_SERVER["REMOTE_ADDR"]); // Will still cause the error/notice message 

To avoid the message, use:

 $r = array_key_exists( 'REMOTE_HOST', $_SERVER) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr($_SERVER["REMOTE_ADDR"]); 
+2
source share

I ran into this problem when using PHPUnit. Here's how I do it:

  $_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'; 
+2
source share

Change httpd.conf on your web server, add this line HostnameLookups On at the end of the file, save and restart the server.

0
source share

The best way:

 $isp = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr($datosVista['ip']); 
-2
source share

All Articles