I want to get visitors country via their IP... Right now I'm using this (http://api.hostip.info/country.php?ip=...... )
Here is my code:
<?php
if (isset($_SERVER['HTTP_CLIENT_IP']))
{
$real_ip_adress = $_SERVER['HTTP_CLIENT_IP'];
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$real_ip_adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$real_ip_adress = $_SERVER['REMOTE_ADDR'];
}
$cip = $real_ip_adress;
$iptolocation = 'http://api.hostip.info/country.php?ip=' . $cip;
$creatorlocation = file_get_contents($iptolocation);
?>
Well, it's working properly, but the thing is, this returns the country code like US or CA., and not the whole country name like United States or Canada.
So, is there any good alternative to hostip.info offers this?
I know that I can just write some code that will eventually turn this two letters to whole country name, but I'm just too lazy to write a code that contains all countries...
P.S: For some reason I don't want to use any ready made CSV file or any code that will grab this information for me, something like ip2country ready made code and CSV.
Use MaxMind GeoIP (or GeoIPLite if you are not ready to pay).
I tried Chandra's answer but my server configuration does not allow file_get_contents()
PHP Warning: file_get_contents() URL file-access is disabled in the server configuration
I modified Chandra's code so that it also works for servers like that using cURL:
Hope that helps ;-)
I have a short answer which I have used in a project. In my answer, I consider that you have visitor IP address.
You can use a web-service from http://ip-api.com
in your php code, do as follow :
the query have many other informations:
Not sure if this is a new service but now (2016) the easiest way in php is to use geoplugin's php web service: http://www.geoplugin.net/php.gp:
Basic usage:
They also provide a ready built class: http://www.geoplugin.com/_media/webservices/geoplugin.class.php.tgz?id=webservices%3Aphp&cache=cache
We can use geobytes.com to get the location using user IP address
it will return data like this
Function to get user IP