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?

- 1
- 1

- 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 Answers
- 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.
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)

- 68,075
- 43
- 96
- 126
-
Since that is so as what I thought. Is there a good reason to use `REMOTE_ADDR` instead of `SERVER_ADDR`? – Question Overflow Mar 23 '14 at 11:04
-
I think you are still confused. Did you see the example I have put up ? – Shankar Narayana Damodaran Mar 23 '14 at 11:06
-
Yes, I saw what you wrote. Am I wrong to say that `REMOTE_ADDR` is the client's ip? And shouldn't the script be checking the server's ip to determine what environment it is in? – Question Overflow Mar 23 '14 at 11:08
-
You are right, `REMOTE_ADDR` refers to the client's IP address. If you check this example on your local environment, you will getting the same addresses..but it differs when you check this from your live server. – Shankar Narayana Damodaran Mar 23 '14 at 11:12
-
1
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.

- 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
-