PHP

Get City Country By IP Address in PHP

This tutorial describe how to get city or country name by IP address.  I used geoplugin.net library in this script. This code will not work on localhost.
View Demo

/*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/>';
View Demo
Download Source Code(Get City Country By IP Address in PHP)
Size: 27 KB
Version: V 1

Recommended Tutorials for you


Related posts

Hit counter in PHP

Anuj Kumar

How to upload and validate a image in php

Anuj Kumar

Server Side form Validation in PHP

Anuj Kumar

How to get Browser Information In PHP

Anuj Kumar

How to get current page URL in php

Anuj Kumar

How to change Password in php

Anuj Kumar

Time Ago Script

Anuj Kumar

jQuery Dependent DropDown List – States and Districts

Anuj Kumar

12 comments

princy December 12, 2018 at 3:23 pm

sir i want image processing project in php pls send me

Reply
Anuj Kumar December 12, 2018 at 5:07 pm

Not available

Reply
Dharmendra November 10, 2018 at 6:22 am

Your Same Code Used in my Website but it give me this
IP Address:- 103.216.215.125
City:- Not Define
Country:- Not Define

Reply
pritty varghese November 10, 2018 at 6:22 am

hii… its not working when I implement my code

Reply
Anuj Kumar November 10, 2018 at 6:34 am

while using this code you must connect with internet

Reply
pritty varghese November 10, 2018 at 6:44 am

put this code in live project.. But its not working

Reply
Anuj Kumar November 10, 2018 at 9:02 am

what error you are getting

Reply
Dharmendra September 19, 2018 at 7:45 am

Your Same Code Used in my Website but it give me this
IP Address:- 103.216.215.125
City:- Not Define
Country:- Not Define

Reply
pritty varghese January 25, 2018 at 5:44 am

hii… its not working when I implement my code

Reply
Anuj Kumar January 25, 2018 at 6:19 am

while using this code you must connect with internet

Reply
pritty varghese January 25, 2018 at 7:24 am

put this code in live project.. But its not working

Reply
Anuj Kumar January 25, 2018 at 9:00 am

what error you are getting

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.