Forum Moderators: coopster

Message Too Old, No Replies

Problems obtaining user IP when using PHP include

         

irock

5:10 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I tried to obtain a user IP address inside a PHP include file, the primary program which includes $REMOTE_ADDR variable always returns the server IP. Is there a way to obtain user IP inside the included file? I know the primary program allows perfect IP retrieval, but I cannot put the REMOTE_ADDR inside the primary program for many reasons.

Could anyone pls help?

andreasfriedrich

5:15 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_SERVER['REMOTE_ADDR'] should work. Itīs a super-global so should have global scope anywhere.

Andreas

Nick_W

5:19 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In fact, it shouldn't be a scope issue should it? - I know that some of the old variables like $REMOTE_ADDR do function oddly in newer versions of PHP...

Nick

irock

5:20 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... doesn't seem to work. I included the file containing $ip = $_SERVER['REMOTE_ADDR']; into another PHP program, but the result is still my server IP. Is there a way to get around this?

andreasfriedrich

5:22 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you are including the file on your server via http? If so try using the filesystem instead.

andreasfriedrich

5:24 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick_W [webmasterworld.com] wrote at 05:19 AM on Feb. 12, 2003 in message #3 [webmasterworld.com]
In fact, it shouldn't be a scope issue should it? - I know that some of the old variables like $REMOTE_ADDR do function oddly in newer versions of PHP...

True, locally included files inherit the variable scope of the including file, but I never trusted those register_globals_on globals and Iīm not sure it really worked that way.

Andreas

irock

5:28 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



andreasfriedrich,

How do I include using filesystem?

Thanks!

Nick_W

5:29 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



include('/path/to/include.php');

Nick

irock

5:38 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I tried to include using the file system as well.
include('/path/to/include.php');

$ip = $_SERVER['REMOTE_ADDR']; inside the included file still shows the server IP.

Any other methods to obtain user IP?

Thanks!

Nick_W

5:39 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what happens when you run a file like this:

<?
print($_SERVER['REMOTE_ADDR']);
?>

Nick

irock

5:44 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I run the included file seperately, it returns the user IP. If I run this file after being included into another program, I get my server's IP address.

Nick_W

5:45 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I'm lost. Anyone else?

Nick

irock

5:48 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW, is there a way to 'capture' the entire HTTP output and 'paste' into the program?

Right now, include only, well, includes the codes...

Is there some file functions that allow me to capture the HTTP output of this file? If this can done, I believe the IP thing will return to normal.

Anyone?

andreasfriedrich

6:50 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php
echo $_SERVER['REMOTE_ADDR'], "<br>";
include_once('test.php');
?>

outputs the same ip on both PHP [php.net] 4.1 and PHP [php.net] 4.2.

Nick_W

6:52 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for the sake of clarity irock, can you post the files minus all the unneeded code?

Nick

felix

6:59 pm on Feb 12, 2003 (gmt 0)

10+ Year Member



When I run the included file seperately, it returns the user IP. If I run this file after being included into another program, I get my server's IP address.

I am not sure what you are doing. Let's say you are including "file1.php" into another program "file2.php". If your file2.php was launched in response to the same request from the same client, all the globals should be the same. The only time $_SERVER["REMOTE_ADDR"] should contain the ip address of the server is when the server requested the file. Do you think that may be happening?

andreasfriedrich

7:45 pm on Feb 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



felix [webmasterworld.com] wrote at 06:59 AM on Feb. 12, 2003 in message #16 [webmasterworld.com]
The only time $_SERVER["REMOTE_ADDR"] should contain the ip address of the server is when the server requested the file.

Thatīs why irock tried to include the file using the filesystem instead of http. It still showed the serverīs ip address.

Andreas

crypto

3:50 am on Feb 13, 2003 (gmt 0)

10+ Year Member



I have tried this and it works perfectly for me. Irock, I hope you are not making an http request for the file.

File ip.php:
------------
<?
$ip=$REMOTE_ADDR;
?>

File abc.php
-------------
<?
echo $REMOTE_ADDR."<br>";
include "ip.php";
echo $ip;
?>