8

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

The weird thing is, when I restart apache and load the page, it works exactly once, if I reload the site + all the times after that, it's empty. I've had other people try, same result, empty.

someone suggested it was something with IPv6 configuration, I have now completely disabled IPv6 but the problem persists.

Marius Prollak
  • 368
  • 1
  • 7
  • 22

3 Answers3

3

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

onionpsy
  • 1,502
  • 11
  • 15
2

Yes it's possible for REMOTE_ADDR to be empty. so if you want you can use this code that I use to get the 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'm checking REMOTE_ADDR first as it is more reliable and sometimes HTTP_X_FORWARDED_FOR can be spoofed by users

fedmich
  • 5,343
  • 3
  • 37
  • 52
  • I think it is better to use `isset($_SERVER['HTTP_X_FORWARDED_FOR'])` rather than `empty($_SERVER['HTTP_X_FORWARDED_FOR'])` – Andrew Sep 22 '15 at 20:36
  • you know the difference with isset and empty right? When I looked before on my server's log. I've encountered some browser still sending http_x_forward_for but it's blank. so I think its empty() is needed – fedmich Sep 23 '15 at 02:00
  • actually it is best to use both... because it is possible that clients might not using proxy so they do not have the `$_SERVER['HTTP_X_FORWARDED_FOR']` variable set....so by just check empty will lead to some issues when php parse it....imho.. – Andrew Sep 23 '15 at 05:35
  • Andrew, empty is using both isset() and checking if not blank string. it won't throw php warning too. Maybe you're thinking of it the other way around... http://php.net/manual/en/function.empty.php Cheers mate – fedmich Sep 23 '15 at 07:23
  • thanks for ur reminder..I always thought empty will only check for ..empty as the function name stated...:) to me its more readable `isset + empty` :) – Andrew Sep 23 '15 at 07:54
1

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

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

Worked on the old server of course and I was scratching my head for a while thinking there was something wrong with Apache..Was there a module missing? Some obscure Apache setting I can't find?. I thought Apache wasn't sending on the Server variables. That was until I tried the plain ol' fashioned way and it worked:

echo $_SERVER['REMOTE_ADDR'];

Turned out I had to edit php.ini and set short_open_tag to On. Facepalm - the shortcut php tags weren't working. Hopefully that will help save someone else a head of time :)

Bastion
  • 761
  • 10
  • 10