Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message: Undefined index: REMOTE_HOST in $_SERVER

Tags:

php

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.

like image 933
TCM Avatar asked Nov 14 '10 18:11

TCM


4 Answers

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"]);
like image 61
mario Avatar answered Oct 23 '22 10:10

mario


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.

like image 21
codaddict Avatar answered Oct 23 '22 09:10

codaddict


$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 !

like image 33
user2038893 Avatar answered Oct 23 '22 10:10

user2038893


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';
like image 2
Thiago Mata Avatar answered Oct 23 '22 10:10

Thiago Mata