Page 1 of 1

How to distinguish client ip from server ip on localhost

Posted: Thu Feb 27, 2014 6:52 pm
by Mimouch
Hi,
I am working with a php script that did different tasks based on the ip accessing it with, the script looks like that :

Code: Select all

if ($_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'])
{ // do tasks here }
else
{ // do some other tasks here }
I am working on ubuntu in localhost, both $_SERVER['SERVER_ADDR'] and $_SERVER['REMOTE_ADDR'] return always 127.0.0.1. I need some trick to have $_SERVER['REMOTE_ADDR'] different to simulate a live site example.

Any idea ?
Thank you

Re: How to distinguish client ip from server ip on localhost

Posted: Thu Feb 27, 2014 9:52 pm
by requinix
Then set it somewhere.

Code: Select all

$_SERVER['REMOTE_ADDR'] = '12.34.56.78';
Kinda unsure about this whole thing though. They'll only be equal when the computer you're using is the same one with the website, and then it'll be the localhost address (127.0.0.1) every time.

Re: How to distinguish client ip from server ip on localhost

Posted: Fri Feb 28, 2014 7:50 am
by Mimouch
Hi; in test1.php i have :

Code: Select all

<?php
header("Location: /test2.php");
?>
in test2.php i have :

Code: Select all

<?php
$req = $_SERVER['REMOTE_ADDR'] . "=" . $_SERVER['SERVER_ADDR'] . "\n";
file_put_contents("ipsfrst.txt", $req);
?>
In this case and when a normal visitor visits the test1.php, the ips that will be recorded by test2.php will be different isen't it ? Because i was thinking that because the server who will redirect than it is the server who do the action than it is its ip that will be the remote one instead of the client ip. So please correct me if i am wrong.
Thank you.

Re: How to distinguish client ip from server ip on localhost

Posted: Fri Feb 28, 2014 8:40 am
by Mimouch
The question can be samplified to that :

In which case $_SERVER['REMOTE_ADDR'] and $_SERVER['SERVER_ADDR'] will return the seem value ? of course i am talking about live site not localhost.

Re: How to distinguish client ip from server ip on localhost

Posted: Fri Feb 28, 2014 1:08 pm
by requinix
They won't.

The SERVER_ADDR is the address of the server (which may be an internal or external address). The REMOTE_ADDR is the IP address of the visitor to the site. As such a redirect won't change either of those because the server and visitor are the same as before.