You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial). Because of this, it is possible that our website may not appear correctly in your browser. We apologise for the inconvenience, and recommend you upgrade your browser to one which is compatible with CSS. For more information, please visit our Browser Upgrade page.

4WebHelp

Scripts: PHP: Your IP

This script will output the IP of the visitor. In some cases, $REMOTE_ADDR needs to be replaced with $HTTP_X_FORWARDED_FOR. This code can be used in any page which is parsed by PHP (usually with a .php extension).

<?php

###########################################################################
#                                                                         #
# Neither http://www.4webhelp.net/ nor its members accept any             #
# responsibility, either expressed or implied, for any damage caused by   #
# using this script or the misuse of this script.                         #
#                                                                         #
#                                                                         #
#                          INSTRUCTIONS                                   #
#                                                                         #
# 1) Copy this code to an editor such as Notepad and save it with a       #
# .php  extension.                                                        #
# 2) FTP this file to a folder on your site in ASCII mode                 #
# 3) Call up this file in your web browser to see your IP                 #
#                                                                         #
###########################################################################

print ('Your IP is: '.$_SERVER['REMOTE_ADDR'].'.');

?>

<p>This script courtesy of <a href="http://www.4webhelp.net/">4WebHelp</a>.</p>
Latest comments
Joshua K Roberson
Here are different ways to store an IP or an IP range as well as query for the IPs.

http://strictcoder.blogspot.com/2009/08/different-ways-to-query-for-ip-in-your.html
Marc
Whops... Should have been as follows (missed the r out of the $rip in the echo statement!)


<?php

//GLOBALS OFF WORK ROUND
if (!ini_get('register_globals')) {
$reg_globals = array($_POST, $_GET, $_FILES, $_ENV, $_SERVER, $_COOKIE);
if (isset($_SESSION)) {
array_unshift($reg_globals, $_SESSION);
}
foreach ($reg_globals as $reg_global) {
extract($reg_global, EXTR_SKIP);
}
}

//FIND THE VISITORS IP      
     if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
     {
        $rip = getenv("HTTP_CLIENT_IP");
     }
     else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
     {
        $rip = getenv("HTTP_X_FORWARDED_FOR");
     }
     else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
     {
        $rip = getenv("REMOTE_ADDR");
     }
     else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
     {
        $rip = $_SERVER['REMOTE_ADDR'];
     }
     else
     {
        $rip = "unknown";
     }

//DISPLAY THE VISITORS IP
echo "Your IP is $rip";

?>
Marc
You could always use this to display a visitors IP, it works with globals off using a work round and checks the HTTP_CLIENT_IP, then if that is unknown it checks HTTP_X_FORWARDED_FOR, then if that is unknown it checks REMOTE_ADDR and finally if that is unknown it gives up and says it is unknown!


<?php
//Globals off work round
if (!ini_get('register_globals')) {
$reg_globals = array($_POST, $_GET, $_FILES, $_ENV, $_SERVER, $_COOKIE);
if (isset($_SESSION)) {
array_unshift($reg_globals, $_SESSION);
}
foreach ($reg_globals as $reg_global) {
extract($reg_global, EXTR_SKIP);
}
}

//Find the IP of the visitor    
     if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
     {
        $rip = getenv("HTTP_CLIENT_IP");
     }
     else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
     {
        $rip = getenv("HTTP_X_FORWARDED_FOR");
     }
     else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
     {
        $rip = getenv("REMOTE_ADDR");
     }
     else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],

"unknown"))
     {
        $rip = $_SERVER['REMOTE_ADDR'];
     }
     else
     {
        $rip = "unknown";
     }

//Display the IP of the Visitor
echo "Your IP is $ip";

?>
jz
or even...


<?php

$ip = getenv(REMOTE_ADDR);

print "Your IP Address is ".$ip;


?>
Ryan Carson - Refresh Creations
Of course, you should be using echo which is twice as fast as print.
mortideus
To show the IP in the title, you either use the listed code within the <title> tags or call it before the title tags are listed and store it to a variable.
Ex. <title><?php print ($_SERVER['REMOTE_ADDR']); ?></title>
RoYeti
is it posible to show the ip in title? how to to that ?
czehu
There is an obvious mistake in the example. The client IP is stored in the $_SERVER array, not $_GET
hmmm
how do i show there proxy?
klaw
Just add the code somewhere inside the web page code. You may have to change the page extension name from .htm(l) to .php.
Tommy
how would i put this on a website itself?
badger
Hi,

Is there any way to track the IP's?

I'm working on a local machine and know all my users IP's and names. Can I add this to a database so I can see whos using what pages etc?

Is there a application which can help me with?

Thanks

Badger
Daniel, 4WebHelp Team Member
Replace
<?php

print ('Your IP is: '.$REMOTE_ADDR.'.');

?>
with
<?php

print ('<font size="X">Your IP is: '.$REMOTE_ADDR.'.</font>');

?>
where X is the font size between 1 and 5.
Paul
How would you change the size that it appears?
philip
Keep in mind that the above script requires the php directive register_globals = on, you should use $_SERVER['REMOTE_ADDR'] instead.  Also, many times $_SERVER['HTTP_X_FORWARDED_FOR'] is an array, not a string, so you should keep that in mind too Wink

Add a new comment

This page is © Copyright 2002-2024, 4WebHelp. It may not be reproduced without 4WebHelp's prior permission.