How to distinguish client ip from server ip on localhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mimouch
Forum Newbie
Posts: 3
Joined: Thu Feb 27, 2014 6:44 pm

How to distinguish client ip from server ip on localhost

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Mimouch
Forum Newbie
Posts: 3
Joined: Thu Feb 27, 2014 6:44 pm

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

Post 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.
Mimouch
Forum Newbie
Posts: 3
Joined: Thu Feb 27, 2014 6:44 pm

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply