PHP getenv(‘REMOTE_ADDR’) serious side effects

ipip addressPHP

I am trying to grab a users' IP address to show them content relevant to their geography. I used the php code $ip = getenv('REMOTE_ADDR'); to grab their IP address. This works fine on my computer but my co-developer tried it and found that he was unable to connect to our site at all and got a "Host Not Accessible" error. He is unable to connect to our site via FTP as well or from another computer at his house.

I took out this code and he still couldn't connect (but could using a proxy). I am wondering why this code would have these side effects. Is $_SERVER('REMOTE_ADDR'). Whats the best way to grab a user's IP address in PHP?

Thanks

Russ

Best Answer

  1. That code doesn't have side effects. The problem your coworker is seeing is more likely a firewall/connectivity/server configuration issue.
  2. Using $_SERVER is indeed the proper way to get those variables' values.