4

I refer to this question regarding the checking of a server's environment with PHP. Why is the REMOTE_ADDR being used instead of SERVER_ADDR when checking whether a server is in a production environment or a development environment? Sorry, I am confused here because I always thought REMOTE_ADDR refers to the client's IP address. Would it be better to use SERVER_ADDR or is there some other reason why REMOTE_ADDR is chosen?

Community
  • 1
  • 1
Question Overflow
  • 10,925
  • 18
  • 72
  • 110
  • The `$_SERVER['REMOTE_ADDR']` returns the IP address from which the user is viewing the current page. And `$_SERVER['SERVER_ADDR']` returns the IP address of the server under which the current script is executing. – underscore Mar 23 '14 at 11:01

2 Answers2

2
  • The SERVER_ADDR returns the IP address of the server under which the current script is executing.
  • THE REMOTE_ADDR returns the IP address from which the user is viewing the current page.

You should read the manual.

Assume that the below script is running on your server..

<?php
echo $_SERVER['SERVER_ADDR'];
echo $_SERVER['REMOTE_ADDR'];

The first line prints your server's IP address (This will not change unless you move your script to some other server). The second line prints the IP address of user who is currently viewing this page.(This will be changing for different users connected from different pcs)

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

It's fairly simple. The reason one of the answers in the question you're mentioning suggest to look att the IP of the client is that if the client is visiting from 127.0.0.1 to figure out if the server is in dev or prod is that no one would be visiting from 127.0.0.1 on a production server.

127.0.0.1 is localhost if I as the client have the IP 127.0.0.1 I' m visiting from the machine the site is running on and thus I'm not an external visitor.

This means that I can have a server on my local machine while I'm developing.

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • Can I conclude from the above that it makes no difference between using `REMOTE_ADDR` and `SERVER_ADDR`? – Question Overflow Mar 24 '14 at 02:49
  • No, no you can not. But localhost is Always 127.0.0.1 unless you take some measures against it. Thus If my clients IP is 127.0.0.1 the client is visiting from the same machine that the server is running on. Which wouldnt be the case for the target audience. – Daniel Figueroa Mar 24 '14 at 08:01
  • Then can I say that if the client is visiting from a private ip, the server must also be on a private ip. And on the other hand, if the client is visiting from a public ip, then the server must also be on a public ip. So, even though they may not be the same ip, the idea is that it is sufficient to use this basis to determine the environment of the server and that it doesn't really matter whether I check for `REMOTE_ADDR` or `SERVER_ADDR`? – Question Overflow Mar 24 '14 at 08:30
  • I'm sorry but I don't understand your question. – Daniel Figueroa Mar 24 '14 at 11:57