spicykimchi
spicykimchi

Reputation: 1151

How To Track the Real IP Address Behind the Proxy

How can we track the Real IP address behind the proxy using PHP?I mean a pure PHP implementation.Because they can turn off the js of their browsers.BTW when the JS is turn on I may use HTML 5 geolocation so I don't need the IP address to locate the user.

Upvotes: 8

Views: 28302

Answers (3)

Cogicero
Cogicero

Reputation: 1524

This PHP code will work, and is useful for forward and reverse proxies. For reverse proxies, note that the X_FORWARDED_FOR header information, if available at all, can be forged and should not be trusted at all.

if($_SERVER["HTTP_X_FORWARDED_FOR"] != ""){
   $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
   $proxy = $_SERVER["REMOTE_ADDR"];
   $host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}else{
   $IP = $_SERVER["REMOTE_ADDR"];
   $proxy = "No proxy detected";
   $host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
}

Upvotes: 10

Quentin
Quentin

Reputation: 944530

You can look at the X-Forwarded-For HTTP header if one is sent, but there is no guaranteed way to know.

Upvotes: 12

Marcin
Marcin

Reputation: 49886

The user who is behind a proxy does not have a globally routable IP address. If you want to maintain session state, you're better off having a session key you generate, and setting it as a cookie, or keeping it in the URL (there are tradeoffs with that approach).

If you really want to find out what their IP address is on their own network, you could probably use javascript in the page to find it out, and then send it back to your server.

Upvotes: 1

Related Questions