a digital blog

PHP, Tutorial

Get City Country By IP Address in PHP

Get City Country By IP Address in PHP

I have received some tutorial requests from readers about, How to Get city and country name using ip address in php? So here is a very small script, it is just four lines of code, using geoplugin.net library. Just remember one thing, this script will not work in your local box.

To get real ip address of user you can follow my tutorial http://www.91weblessons.com/getting-the-real-ip-address-of-website-visitors-in-php/

 

Download Link   Demo Link

 

/*Get user ip address*/
$ip_address=$_SERVER['REMOTE_ADDR'];

/*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
$addrDetailsArr = unserialize(file_get_contents($geopluginURL)); 

/*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city']; 

/*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];

/*Comment out these line to see all the posible details*/
/*echo '<pre>';
print_r($addrDetailsArr);
die();*/

if(!$city){
   $city='Not Define';
}if(!$country){
   $country='Not Define';
}
echo '<strong>IP Address</strong>:- '.$ip_address.'<br/>';
echo '<strong>City</strong>:- '.$city.'<br/>';
echo '<strong>Country</strong>:- '.$country.'<br/>';

9 Comments

  1. Armson

    Perfect..

  2. Lars Johansson

    Fall in love with your idea.. Thank you so much

  3. wadmiraal

    I don’t think it is the right way.
    Instead of this try to use google api.

  4. Billy Sofyan

    Helped to me, but not sure it will works always

  5. Savita Rao

    Nice article

  6. Manfred Woitke

    Thanks for sharing. That’s a nice gadget.

  7. Carp Clash

    Working Perfect.
    Thanks for sharing :)

  8. Simon Ruckus

    WOW!!! Nice Post

  9. James Olsen

    THANX FOR SHARING THIS WILL HELP ME ALOT

4 Pingbacks

  1. Sagar Panchal
  2. Malik Ahmed
  3. Tracy Gorman
  4. Billy


*

Copyright © 91 Web Lessons - a digital blog