85

Possible Duplicate:
Function to get user ip address

<?PHP

$ipaddress = $_SERVER["REMOTE_ADDR"];

echo "Your IP is $ipaddress!";

?>

I was told this way of getting ip address has issues such as being able to fool. Is there a better way to gather ip address? looking for tutorials of better way to get ip address?

Community
  • 1
  • 1
user62617
  • 1,796
  • 7
  • 18
  • 22
  • 1
    Nope, there isn't! Might be able to fool it, but I have never seen it in practice or read it's being done in the masses. – Shef Jul 22 '11 at 19:00
  • 3
    @genesis - but this question is messed up a little and not so clear. I think there is no need to use anything else than just $_SERVER["REMOTE_ADDR"] . There isn't anything better. The fooling of IP address would be done at IP level so there's no way you can solve/detect it in PHP. – Tomas Jul 22 '11 at 19:16

1 Answers1

143

$_SERVER['REMOTE_ADDR'] is the only reliable IP address you'll get - it's extracted directly from the TCP stack and is where the current connection was established from. This means if the user is connecting via a proxy, you'll get the proxy's address, not the user's.

Any of the other header-based ones are unreliable, as HTTP headers are trivial to forge. You can use the information from them, if you'd like, as long as you don't TRUST it.

Marc B
  • 356,200
  • 43
  • 426
  • 500