0

There are 2 servers A and B. I have my php script in server B. Now, the client sends some data to server A and server A sends it to server B.

I want to get the IP address on which the request came(that is server A). How can I do it using php?

If I use $_SERVER['REMOTE_ADDR'], I get the clients IP address. If I use $_SERVER['SERVER_ADDR'], I get the null value.

mario
  • 144,265
  • 20
  • 237
  • 291
Sangam254
  • 3,415
  • 11
  • 33
  • 43
  • It's an issue with your webserver configuration or behaviour if it doesn't populate `SERVER_ADDR`. – mario Oct 12 '12 at 10:36
  • are you redirecting the client browser to server B? – Vipin Jain Oct 12 '12 at 10:39
  • Possible duplicate: go with Question below, http://stackoverflow.com/questions/3003145/how-to-get-client-ip-address – Sujeet Oct 12 '12 at 10:42
  • @Sujeet Nope, it isn't a duplicate of that, more people need to read a question before they assume duplicate entries. However I agree with mario I think this is a server configuration problem, specifically normally due to not setting up the hosts configuration right. – Sammaye Oct 12 '12 at 10:43
  • @VIPIN JAIN I am not redirecting the client browser. The system is such that, I have a 3rd party(server A) processing the client request and sending required data to me(server B). I need to authenticate that the data is coming from some specific IPs which belong to the third party – Sangam254 Oct 12 '12 at 10:47
  • Is this basically a proxy? Some proxies do not populate the server_addr but instead the X_HTTP_FORWARDED_FOR or their own header field. – Sammaye Oct 12 '12 at 10:48
  • @Sangam254 If you want to do this dynamically, verifying the incoming IP address against the known DNS name of the source server is your best bet. The information you desire is not likely to be included in the forwarded request in a form that can be relied upon. If the IP address of the remote server is known, you could just hard code it - this would be the simple option as long as it does not change regularly. – DaveRandom Oct 12 '12 at 10:48
  • Then your server A's configuration must be set to redirect the client IP address in the request headers to server B – Vipin Jain Oct 12 '12 at 10:57
  • @DaveRandom I don't think the 3rd party is passing me the domain names. They have sent a set of IP addresses. – Sangam254 Oct 12 '12 at 10:59
  • @Sammaye I am not sure if there is some proxy involved. So I checked it in another server (sure there is no proxy) and found that it gives a SERVER_ADDR and HTTP_HOST same value 107.x.x.x and SERVER_NAME as 10.x.x.x . I don't know which one I should use. – Sangam254 Oct 12 '12 at 11:04
  • @Sangam254 Are the domain names variable? Surely you can just hard code the DNS name of server A, otherwise you have nothing to verify against no matter how you do it. It would also be worth your showing a `print_r($_SERVER);` for the request you receive. – DaveRandom Oct 12 '12 at 11:18
  • @DaveRandom I would like to add one more thing. It is an HTTP post from server A to server B. So can I use REMOTE_ADDRESS to get the IP address of server A. – Sangam254 Oct 12 '12 at 11:26
  • Yes you can, every POST will have a sender in this case it should be server A – Sammaye Oct 12 '12 at 11:32
  • @Sammaye, Thanks i wasn't careful enough. – Sujeet Dec 13 '12 at 04:27

1 Answers1

0

$_SERVER['SERVER_NAME'] and $_SERVER['HTTP_HOST'] should return the DNS name of the server. If you can use the DNS instead of the IP I think maybe you should try with one of these variables. Although, as mentioned earlier, if $_SERVER['SERVER_ADDR'] isn´t populated you probably have a faulty configuration.

Provide information about server, IIS/Apache, OS, PHP version, it might be easier to help.

Also, try echoing/logging $_SERVER['SERVER_ADDR'] on server A where the request comes in and see if you get a value from there because reading $_SERVER['SERVER_ADDR'] on server B should give you IP of server B. If you get a value on server A you can save/pass the value forward to server B in code (wherever your transaction from server A to B takes place).

"The IP address of the server under which the current script is executing."

Marcus
  • 8,230
  • 11
  • 61
  • 88
  • Will REMOTE_ADDR give me(server B) the IP address of server A or that of client. Coz for server B, server A is the client. I feel if my server configuration is set right, theoretically REMOTE_ADDR should give the IP of server A – Sangam254 Oct 12 '12 at 11:08
  • It all depends on how you are passing the values. If you are passing values from server A to B through a HTTP request, then $_SERVER['REMOTE_ADDR'] on server B should give you address of server A. – Marcus Oct 12 '12 at 11:11
  • It will be a HTTP post form server A to server B. So Can I use REMOTE_ADDRESS to get the IP of server A ? – Sangam254 Oct 12 '12 at 11:24
  • Yes, in that case, I can´t see why not. $_SERVER['REMOTE_ADDR'] on server B should in this case give you the address of server A. Verify this by checking the actual IP on server A and compare with $_SERVER['REMOTE_ADDR'] on server B. They should be the same. – Marcus Oct 12 '12 at 11:31