Request quote

How to get the IP address of users and Country, State and locations

Posted on: June 16th, 2012 by Mohammed Aqeel 6 Comments

Many times we have received the private mails requesting the help to track the IP address of the users. Here is a simple php code to get the IP address of the user. With a little addition to the code, you can get the location details of the user like Country, State and city. Please see the code below.

/*
*       Function to get the users IP address. We "attempt"
*       to determine if the user is coming through a proxy server
*       by checking if HTTP_X_FORWARDED_FOR is set, then we
*       use a regular expression to ensure the IP address
*       found is in the proper format
*/
function getIP(){
        // here we check if the user is coming through a proxy
        // NOTE: Does not always work as proxies are not required
        // to provide this information
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                //reg ex pattern
                $pattern = "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/"
                // now we need to check for a valid format
                if(preg_match($pattern, $_SERVER["HTTP_X_FORWARDED_FOR"])){
                        //valid format so grab it
                        $userIP = $_SERVER["HTTP_X_FORWARDED_FOR"];
                }else{
                        //invalid (proxy provided some bogus value
                        //so just use REMOTE_ADDR and hope for the best
                        $userIP = $_SERVER["REMOTE_ADDR"];
                }               
        }
        //not coming through a proxy (or the proxy
        //didnt provide the original IP)
        else{
                //grab the IP
                $userIP = $_SERVER["REMOTE_ADDR"];
        } 
        //return the IP address
        return $userIP;
}

Usage

Now to show the IP use

<?php echo getIP();?>

How to get the location details from IP?

To get the location details like city, state, country, latitude, longitude etc.. from IP address of user, use the following code along with the above.

<?php
$user_ip = getIP();
$meta_tags = get_meta_tags('http://www.geobytes.com/IPLocator.htm?GetLocation&template=php3.txt&IPAddress=' . $user_ip) or die('Error getting meta tags');

$city = $meta_tags['city'];
$state = $meta_tags['regioncode'];
$country = $meta_tags['fips104'];
$lat = $meta_tags['latitude'];
$long = $meta_tags['longitude'];
?>

Thats it. If you have some alternate ways for getting these details, please post it below.

Tags: , , , , , , , , , ,

6 Responses

  1. madhu says:

    hi Aqeel,

    This code seems not producing ny result.
    Have u tried testing this recently ?

    cheers

  2. Team Webgalli'an says:

    Its working. Which part of the code is not working for you?

  3. satya says:

    Hi

    I am tracking website user but after some time i am getting result
    “Limit Exceeded”.
    but do not show “Limit Exceeded” how do this

    Regards and Thanks

    Satya
    Email -Id: satya2000chauhan@gmail.com

  4. Team Webgalli'an says:

    Check whether you exceeded the Geocode limits.

  5. Ramnandan says:

    Hi i tried out this code,
    The IP address which i am receiving from the above code is different from my real IP address
    I have crosschecked and its displaying “NULL” when i used the same code and tested it online

  6. scorio says:

    It’s not working. it says “Error getting meta tags”