Identifying user's location by IP address

I was writing a program where I wanted the country of the user to be automatically identified. I wrote code that takes the ip and stores it in the DB.

$ip = gethostbyname($_SERVER['REMOTE_ADDR']);


 <?php `$result = mysql_query("INSERT INTO `wh_order` (`name`, `email`, `contact`, `itemid`, `itemquantity`, `ip`,`message`, `date`) VALUES('".$name."','".$email."','".$contact."','".$itemid."','".$itemquantity."','".$ip."','".$message."', NOW())");` ?>

Also, it this the best method of storing ip address?


First of all, gethostbyname won't give you a country. You have to resort to geolocation for that. A free service which works OK is They provide a API:

Which returns:

    "city": "Mountain View",
    "region_code": "CA",
    "region_name": "California",
    "metrocode": "807",
    "zipcode": "94043",
    "longitude": "-122.057",
    "latitude": "37.4192",
    "country_code": "US",
    "ip": "",
    "country_name": "United States"

Note that geolocation by IP wont be 100% accurate.

The best column type to store an IP address is INT(10) unsigned, which will take up less space than a varchar column. Conversion is easily handled with INET_ATON and INET_NTOA:

mysql> SELECT INET_ATON('');
        -> 167773449

mysql> SELECT INET_NTOA(167773449);
        -> ''

You could use the databases of maxmind ( in combination with an apache or nginx extension.

Alternatively, there is a nice library for this task:

Identifiying a user's location by their IP is called Geolocation. It usually requires a database that maps IP address ranges to geographic locations.

The Maxmind company provides such a database called GeoLite for free, under a CreativeCommons license

GeoLite databases are licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Commercial licensing is available.

They have an apache module that allows for quick IP lookups, the easiest way to get started, however, is probably the pure PHP option. Check out the list of possible integration methods.

The download archive includes several example, the basic process is rather straightforward:

$gi = geoip_open("GeoLite.dat", GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, "");
echo $record->latitude;
echo $record->longitude;

Use the services of They support IPV4 and IPV6 addresses to locate an ip's geolocation.


<!DOCTYPE html>
    <title>GEOIP DB - Geolocation by IP</title>
    <script src="">
    <div>Country: <span id="country"></span>
    <div>State: <span id="state"></span>
    <div>City: <span id="city"></span>
    <div>Latitude: <span id="latitude"></span>
    <div>Longitude: <span id="longitude"></span>
    <div>IP: <span id="ip"></span>
            url: "",
            jsonpCallback: "callback",
            dataType: "jsonp",
            success: function( location ) {

This is a jquery snippet, although other examples can be found on their website (Plain javascript, php, C#)

Need Your Help

getting Error in magento

javascript php magento

When I go to my website I receive the following error (I downloaded this error file from the var folder):

Replace article with div with a button that will stay and can toggle between article and div content

javascript jquery html css

I want with jQuery when I click the button to toggle the content between the article and the div.