Here I am going to show you how to get users country details from user IP address.There are different types of methods are available for doing that.But I will show you two ways you can can access users country code,country name etc.

how to get users country details from user IP address
how to get users country details from user IP address

First one is :We can use API for identifieing the users details.Simply there are lots of online sites are giving for API for indentifying users country details.Amongs them http://www.geoplugin.net/ is one So here I will show you how to get the users country details through API by using the site http://www.geoplugin.net/.

Second one is : We can create table in our database then save the data into table after that we can access the users country name and country code from table by using IP address.

First Method :

In the first method I have created a method “ipDetails” that will use the API from website http://www.geoplugin.net/. by providing the IP address. like http://www.geoplugin.net/json.gp?ip=” . $ip.If we put the url http://www.geoplugin.net/json.gp?ip=ip_address in the browser then this site will provide us full details of that IP.
Also we can call our custom method by the way like

$countryName = ipDetails(‘Visitor’, “Country”);
$countryCode = ipDetails(‘Visitor’, “Country Code”);
$state = ipDetails(‘Visitor’, “state”);
$city = ipDetails(‘Visitor’, “city”);
$address = ipDetails(‘Visitor’, “Address”);
$location = ipDetails(‘Visitor’, “Location”);
I we have IP address then we can get all details about that ip address through this methods also like:

$ip = ‘1003.58.15.26’;
$countryName = ipDetails($ip, “Country”);
$countryCode = ipDetails($ip, “Country Code”);
$state = ipDetails($ip, “state”);
$city = ipDetails($ip, “city”);
$address = ipDetails($ip, “Address”);
$location = ipDetails($ip, “Location”);

Also we can get ip address of user if the user visiting to site through PHP $_SERVER variable.Here I have used a method called “getIp()” that will provide the ip address of visiter of the site.

Here I can simply use the getIp() method that will give the IP address of the visiter.Then I can use that IP for getting all details of user.
like :

$ip = getIp();

then after

$countryName = ipDetails($ip, “Country”);

So finaly I will get the country name of user by using all above methods through API.

Second Method :

In this case you have to create a table which will contain country list with ranges of assigned IP-addresses.Here I have used data from MaxMind GeoIP— GeoLite Country Database (CSV file).I have used that CSV file in my table and exported with sql file you can use that sql file which is available in GitHug.

This sql file contains 6 columns: first two columns are contain IP ranges in netblock — string of dot-separated octets. Columns 3-4 are contain IP-ranges in integer format and columns 5-6 are contain country code and country name.

The sql table I have created is like below :

We can download updated CSv file from http://dev.maxmind.com/geoip/legacy/geolite/ then after upload that csv file into mysql database with name of country_with_ip.
like above getIp() method we can get the visiters IP address by simply calling that method by $ip = getIp();

And then after we can use that $ip varibale sql query purpose like below :

We can execute that query by a simple method called getCountry($ip) like below :
$countryName = getCountry($ip);
And that method would be :

Make sure we have connected the database by the following method like :

Hope you enjoyed the post with me.So please don’t be selfish ,If get benefit then please share this post with others.

how to get users country details from user IP address
Tagged on:         

Leave a Reply

Your email address will not be published.