PHP $ _SERVER ['REMOTE_ADDR'] is empty

Somehow $_SERVER['REMOTE_ADDR'] returns an empty string, I have the same code (as part of a script) running on several servers, and it works everywhere, they are all the same.

It is strange when I restart apache and load the page, it works exactly once, if I restart the site + all the time after that, it is empty. I had other attempts, the same result, empty.

someone suggested that this is something with IPv6 configuration, now I have completely disabled IPv6, but the problem persists.

+6
source share
4 answers

if you are behind a proxy server, you can use $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['HTTP_CLIENT_IP'] instead of $_SERVER['REMOTE_ADDR'] . it depends on how your proxy is configured.

+2
source

Yes, REMOTE_ADDR may be empty. so if you want, you can use this code, which I use to get ip based on HTTP_X_FORWARDED_FOR

 <?php if(! empty($_SERVER['REMOTE_ADDR']) ){ $ip = $_SERVER['REMOTE_ADDR']; } else{ $ip = empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? '' : $_SERVER['HTTP_X_FORWARDED_FOR']; } 

I check REMOTE_ADDR first, as it is more reliable, and sometimes HTTP_X_FORWARDED_FOR can be faked by users

+1
source

I had code on a new virtual machine that looked like this. It is called from a javascript file:

 <?php $hostip = $_SERVER['REMOTE_ADDR']; ?> var myip = "<?=$hostip ?>"; 

Of course, I was working on an old server, and for a while I scratched my head, thinking that something was wrong with Apache. We don’t have a module there? Can I find some obscure Apache parameter ?. I thought Apache was not sending server variables. This was until I tried a simple way and it worked:

 echo $_SERVER['REMOTE_ADDR']; 

It turned out that I needed to edit php.ini and set short_open_tag to On . Facepalm - php shortcuts shortcuts did not work. Hope this helps save someone else head of time :)

0
source

Perhaps this will help. I called the PHP script from the iPhone application to update the database containing the IP address. IP is a new field, added just a few days ago, and it did not update from the iPhone, but it worked correctly with Android. The problem is that the new php script was not used by the iPhone until the phone was turned off and restarted. He kept it in cache.

Does anyone know how to clean these old php files?

0
source

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


All Articles