IP lookup or HTTP referer

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
theclay7
Forum Commoner
Posts: 50
Joined: Wed Feb 19, 2003 3:17 am

IP lookup or HTTP referer

Post by theclay7 »

I want to know if there is a function that I can know the IP of a user.

For HTTP_REFERER, I can get the URL from the user such as http://www.sony.com.

BUT is there any way I can also know the IP of that?

thanks.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

not in the $_SERVER array that i know of. try doinf a dns look up on it
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have a look at gethostbyname() in the manual.
dataangel
Forum Newbie
Posts: 12
Joined: Thu Aug 07, 2003 7:29 pm

Post by dataangel »

Code: Select all

$ip = getenv ("REMOTE_ADDR"); // get the ip number of the user
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Cross-version function for retrieving the IP address w/ hostname cleanup ;)

Code: Select all

<?php
    /*
    kriek at jonkriek dot com
    */
    function getIP() {
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                return $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                return $_SERVER['REMOTE_ADDR'];
            }
        } else {
            if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDER_FOR'])) {
                return $GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDED_FOR'];
            } else {
                return $GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'];
            }
        }
    }
    $user = getIP();
    $fullhost = gethostbyaddr($user);
    $host = preg_replace("/^[^.]+\./", "*.", $fullhost);
?>

Your IP address is <?=$user?> | Your host is <?=$host?>
Your IP address is 24.26.91.100 | Your host is *.tampabay.rr.com
Post Reply