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.
I am using
ipinfodb.com
api and getting exactly what you are looking for.Its completely free, you just need to register with them to get your api key. You can include their php class by downloading from their website or you can use url format to retrieve information.
Here's what I am doing:
I included their php class in my script and using the below code:
Thats it.
Replace
127.0.0.1
with visitors IpAddress.Installation instructions are here, and read this to know how to obtain City, State, Country, Longitude, Latitude, etc...
Try this simple one line code, You will get country and city of visitors from their ip remote address.
You can get visitors country and city using ipstack geo API.You need to get your own ipstack API and then use the code below:
Source: Get visitors country and city in PHP using ipstack API
Check out php-ip-2-country from code.google. The database they provide is updated daily, so it is not necessary to connect to an outside server for the check if you host your own SQL server. So using the code you would only have to type:
Example Code (from the resource)
Output
Actually, you can call http://api.hostip.info/?ip=123.125.114.144 to get the information, which is presented in XML.