What is more reliable gethostbyaddr ($ _ SERVER ['REMOTE_ADDR']) or $ _SERVER ['REMOTE_HOST']

I need to get the name of the remote URL from the server script , which is one of the following more reliable:

gethostbyaddr($_SERVER['REMOTE_ADDR']) or $_SERVER['REMOTE_HOST']

+4
source share
2 answers

This has nothing to do with reliability. Both variables simply do not match, although under certain circumstances they may contain the same value. Let me explain:

 $_SERVER['REMOTE_ADDR'] 

in all cases will contain the IP address of the remote host, where

 $_SERVER['REMOTE_HOST'] 

will contain the DNS host name if DNS resolution is enabled (if the HostnameLookups Apache directive is set to On , thanks @Pekka). If it is disabled, then $_SERVER['REMOTE_HOST'] will contain the IP address, and this is what you might notice.

Your code should look like this:

 $host = $_SERVER['REMOTE_HOST']; // if both are the same, HostnameLookups seems to be disabled. if($host === $_SERVER['REMOTE_ADDR']) { // get the host name per dns call $host = gethostbyaddr($_SERVER['REMOTE_ADDR']) } 

Note. If you can manage the apache directive, I would advise you to disable it for performance reasons and get the host name - only if you need it - using gethostbyaddr()

+9
source

$_SERVER['REMOTE_HOST'] will only be set if the HostnameLookups Apache directive is set to On .

You can check $_SERVER['REMOTE_HOST'] , and if it is not installed, search for the hostname.

Both can be equally reliable as they will use the same search engine inside. For general reliability of this information, see PHP Reliability $ _SERVER ['REMOTE_ADDR']

Remember that finding hosts can be very slow. Do not do them unless you have a good reason.

+7
source

Source: https://habr.com/ru/post/1483753/


All Articles