I am mediating requests from client A to server B. Client A may be a phone or a laptop computer. Server B wants to know client A's IP address, but they are looking for it in $_SERVER['REMOTE_ADDR']
. Because I am hitting server B, they are getting the "wrong" value for $_SERVER['REMOTE_ADDR']
-- they're getting my servers' IP addresses. What can I do on my side so that server B will see the client's IP address when they access $_SERVER['REMOTE_ADDR']
at their endpoint?
Asked
Active
Viewed 6,815 times
2

boo-urns
- 10,136
- 26
- 71
- 107
2 Answers
5
This is happening at a layer that cannot be overridden by your PHP script. The X-Forwarded-For
header exists to relay the true client IP, but the remote server must support this easily-spoofed value.
$header = "X-Forwarded-For: {$_SERVER['REMOTE_ADDR']}, {$_SERVER['SERVER_ADDR']}";
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array($header));

Annika Backstrom
- 13,937
- 6
- 46
- 52
-
+1 Beat me to it :) - In addition, see here for how server B should detect client A's IP (notice REMOTE_ADDR is the last place you look) - http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php – deizel. Oct 03 '12 at 01:12
0
EDIT: Prior answer is perfect. This only applies where your own dev-ip is interfering with client stats.
In the absence of more info, at the most basic level, you should remove your own 'developer IP' so that you're not included in any stats/data/logs...
Your IP may be different to the server in question:
<?php
$dev_ip = '217.12.14.14' // your dev IP address
if ($_SERVER['REMOTE_ADDR'] == $dev_ip){
// do nothing
} else {
// do|show|log something
}
This doesn't account for dynamic IP address allocation (rather than a static IP). Dynamic IP's are often allocated by ISPs which is something that causes further issues.

nickhar
- 19,981
- 12
- 60
- 73
-
2
-
Ahh... So we're talking pass-through of IP to client and acting as a gateway! (sorry, +1 for the comment!) – nickhar Oct 03 '12 at 01:29
-
Upvote to other answer - entirely correct use of X-forwarded-for, and -1 to me for misreading initial question. I missed the gateway element :) – nickhar Oct 03 '12 at 01:34