how to find country name from IP Address??

Discussion in 'Programming' started by LOD, Jul 26, 2009.

  1. #1
    hi can anyone tell me how can i find out a visitors country name by his ip address??
    thanks
     
    LOD, Jul 26, 2009 IP
  2. PureSoul

    PureSoul Active Member

    Messages:
    91
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #2
    An easy free choice is IP2Country database (it uses the free database from MaxMind GeoIP) but free isn't always better. The rest is for you to research. :)
     
    PureSoul, Jul 26, 2009 IP
  3. mioot

    mioot Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can download the database from ip2location.com and it is free .
     
    mioot, Jul 27, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or you can just use two lines of code:

    
    $geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
    $countryCode = $geo['geoplugin_countryCode'];
    
    PHP:
    If your host doesn't support file_get_contents, add _curl to the end of that function and add this function at the top of your script

    
    function file_get_contents_curl($url) {
    	$ch = curl_init();
    	
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    	curl_setopt($ch, CURLOPT_URL, $url);
    	
    	$data = curl_exec($ch);
    	curl_close($ch);
    	
    	return $data;
    }
    
    PHP:
    If you get a ton of traffic, I would recommend assigning cookies to visitors with their country codes so that if they revisit the page/site you can pull that from their cookies before you start pulling it from the geoplugin url.

    Far as the MaxMind stuff goes, to get country code from it, simply download the latest GeoLite Country binary database from here : http://www.maxmind.com/app/geolitecountry and then download geoip.inc from here http://geolite.maxmind.com/download/geoip/api/php/

    Then you use this code (assuming both geoip.inc and GeoIP.dat are in the same folder)
    
    include("geoip.inc");
    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
    $addr = $_SERVER["REMOTE_ADDR"];
    $country_code = geoip_country_code_by_addr($gi, $addr);	
    
    geoip_close($gi);
    
    PHP:
    That would be a local way of doing it if you don't want to hit up one of the servers for each request.

    Far as accuracy goes, country wise its not too big a deal,but city wise you may wish to have more fine tuned accuracy, which maxmind offers the paid database. For example the difference between GeoLite Country and GeoIP Country (paid), is 99.5% accuracy and 99.8%.
     
    Last edited: Jul 27, 2009
    kblessinggr, Jul 27, 2009 IP
  5. yodayoz

    yodayoz Peon

    Messages:
    111
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    there are many websites offering you the service for free like "whatismyipaddress dot com" and many more.
    If you want to integrate the code in your website, you can follow the code as 'kblessinggr" mentioned, or PM me if you want the code for asp.net
    There are some 3rd party sites offering this service for web administrators for free like "ewebcounter dot com"
     
    yodayoz, Aug 5, 2009 IP
  6. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You don't need a whatsmyip service if they're visiting your site since you can get that information right from your webserver.
     
    kblessinggr, Aug 14, 2009 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    Using a database that you download means that when things change your information is out of date. Use kblessinggr's method - an API provided by a geolocation site - and your data will usually be current. (It may not be if you check right after there's a change and they haven't changed the database yet, but countries don't just pop up out of nowhere - we usually have months of notice before there's a change, so they're usually up to date.)
     
    Rukbat, Dec 21, 2012 IP