loging remote ip not working as i require it to

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
toasty525
Forum Newbie
Posts: 10
Joined: Wed Apr 05, 2006 12:48 pm

loging remote ip not working as i require it to

Post by toasty525 »

Hi,

i am trying to log the users ip when they visit one of my sites

the setup is as follows:

i have one page on a site 1 that has the code:

Code: Select all

 
<?PHP
$ip=$_SERVER['REMOTE_ADDR'];
echo $ip;
?>
 
and another page on site 2 that has the code:

Code: Select all

 
<?PHP
include('http://site1.com/gettest.php');
?>
 
When a user visits site 2 i want site 1 to log there IP address but instead it only logs the IP address of the host for site 2.
Any help would be great thanks :D
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: loging remote ip not working as i require it to

Post by jackpf »

I'd recommend using cURL or sockets to open the page on the second server. That way you can pass the user's ip address in the query string, or as a cURL paremeter.

Since the server is making the request to include the file, then the second server will obviously log the first server's IP address.
toasty525
Forum Newbie
Posts: 10
Joined: Wed Apr 05, 2006 12:48 pm

Re: loging remote ip not working as i require it to

Post by toasty525 »

I have found a simple way around it using HTML and an iframe:

Code: Select all

 
<iframe allowtransparency="true" src="http://site1.com/gettest.php" frameborder="0" scrolling="no"></iframe>
 
if i place the above HTML on site 2, site 1 is able to log the IP of the user instead of the IP of the host :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: loging remote ip not working as i require it to

Post by jackpf »

Ahh yeah, I forgot about that.

Another way would to put a link the script on server 1 in an <img> tag with a width and height of 0. Then when the browser goes the fetch to image, it'll execute the script on server 1.

Either way works.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: loging remote ip not working as i require it to

Post by McInfo »

Another option is to pass the IP address through the query string.

Server 2:

Code: Select all

include 'http://server1/test.php?ip='.$_SERVER['REMOTE_ADDR'];
Server 1:

Code: Select all

$_GET['ip']
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 7:02 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: loging remote ip not working as i require it to

Post by jackpf »

Oh yeah, I suppose you're using URL includes. I've never done that since I only do local includes, which uses the filesystem rather than HTTP...so you can't include query strings...

Forgot about that x_x
Post Reply