Forum Moderators: coopster

Message Too Old, No Replies

Code to display visitors IP address on website

php script to display visitors ip address

         

slgdceo2

5:12 pm on Jul 6, 2008 (gmt 0)

10+ Year Member



It seems simple enough, but I cannot find the code to put on my web page that will display the visitors the IP address. Can someone give me the code? I'm no programmer so please explain in detail what I supposed to do. My site is coded in php.

cameraman

5:22 pm on Jul 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, slgdceo2!

Where you want it in the html, put this:
<?php echo $_SERVER['REMOTE_ADDR']; ?>

There's not a lot to explain, echo just means send the following to the browser. $_SERVER is an array with a number of different informative items, with REMOTE_ADDR being the array element that contains the visitor's IP address.

slgdceo2

6:19 pm on Jul 6, 2008 (gmt 0)

10+ Year Member



Thank you! I knew it was simple...but just couldn't get a simple answer from anywhere.

eelixduppy

3:15 am on Jul 7, 2008 (gmt 0)



Just note that this might not be the user's actual IP address, especially if a proxy is being used.

and Welcome to WebmasterWorld! :)

dreamcatcher

7:37 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Correct. If the user is behind a proxy, their IP should be stored in '$_SERVER['HTTP_X_FORWARDED_FOR']'. So you should modify your code like this:

if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
echo $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
echo $_SERVER['REMOTE_ADDR'];
}

dc

slgdceo2

1:33 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



Like I said, I'm no programmer, so it looks as if your asking me to add this code or this is the code, because it looks different than the other. So can you put this all together like a clean package for me and others like me that don't know what they're doing?

cameraman

3:14 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The statements dc posted are ready to go, just put them inside php tags like I did further up:

<?php
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
echo $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
echo $_SERVER['REMOTE_ADDR'];
}
?>

Those statements just saying that if the forwarded field is there, display it, otherwise display the second one.

penders

3:43 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the user is behind a proxy, their IP should be stored in '$_SERVER['HTTP_X_FORWARDED_FOR']'.

Just to note... $_SERVER['HTTP_X_FORWARDED_FOR'] could return multiple IPs of the form:

"11.22.33.44, 11.22.33.55"

- I believe if the user is going through multiple proxies...?

slgdceo2

4:15 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



Ok, thanks guys for your speedy replies. I think the proxy ip address is overkill for now...but i will keep it in consideration. Thanks again. That's the best about WW, you will get a quick response, some forums you have to wait for days to get a response.... Thanks guys!

penders

4:39 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think the proxy ip address is overkill for now...but i will keep it in consideration.

I think it's worth baring in mind that this could also depend on the (configuration of the) server you are installing your script on. I've had $_SERVER['REMOTE_ADDR'] return nothing on a server in a large corporate network. But $_SERVER['HTTP_X_FORWARDED_FOR'] returned the correct information.

dreamcatcher

6:39 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe if the user is going through multiple proxies...?

That is correct. You would probably need a function like this:

<?php
function get_ip_list() {
$tmp = array();
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')) {
$tmp += explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$tmp[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$tmp[] = $_SERVER['REMOTE_ADDR'];
return $tmp;
}
?>

dc

slgdceo2

7:02 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



DC I put in this code above and nothing showed up. The simple code from before worked fine. <?php echo $_SERVER['REMOTE_ADDR']; ?>

dreamcatcher

6:24 am on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$tmp = get_ip_list();
echo implode(",",$tmp);

dc

slgdceo2

2:28 pm on Jul 8, 2008 (gmt 0)

10+ Year Member



I'm no programmer...please give me the whole code as it should be.

eelixduppy

3:19 pm on Jul 8, 2008 (gmt 0)



get_ip_list above is a function that you must call in order to get something. That is what dc is showing you in his last post. So you have the function delaration and then the call afterwards:

function get_ip_list() {
$tmp = array();
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')) {
$tmp += explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$tmp[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$tmp[] = $_SERVER['REMOTE_ADDR'];
return $tmp;
}
echo echo implode(",",get_ip_list());

System

11:48 pm on Jul 9, 2008 (gmt 0)

redhat



The following message was cut out to new thread by eelixduppy. New thread at: php/3694970.htm [webmasterworld.com]
8:24 pm on July 9, 2008 (est -4)