Get visitor's location details from IP address in php

If you want to track your visitor's location details like city, state, country etc then you can use the following script. In PHP we can easily get the visitors/ user IP address by using $_SERVER['REMOTE_ADDR'] option. But you can't get other information like country, city, state etc directly in PHP. Codehelper.io provides the solution for this.

How to get user IP Address in PHP


<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>

How to get user/ visitor Location in PHP

<?php
require_once("userip/ip.codehelper.io.php");
require_once("userip/php_fast_cache.php");

$_ip = new ip_codehelper();

$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

$guest_ip   = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city  = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];

echo "IP Address: ". $guest_ip. "<br/>";
echo "Country: ". $guest_country. "<br/>";
echo "State: ". $guest_state. "<br/>";
echo "City: ". $guest_city. "<br/>";

?>

The above code provides the information like ip, Continent Code, Continent Name, Country Code in 2 digit, Country Code in 3 digit,  Country short code, Country Name, Region / State Name, City Name, City Latitude, City Longitude,  Country Latitude, Country Longitude, Local Time Zone, Calling Code, Population, Area in  Sq Km, Capital, Electrical, Languages, Currency and Flag image.

$ip 			= $visitor_location['IP'];
$Continent_Code 	= $$$visitor_location['ContinentCode'];
$Continent_Name 	= $visitor_location['ContinentName'];
$Country_Code2 		= $visitor_location['CountryCode2'];
$Country_Code3 		= $visitor_location['CountryCode3'];
$Country 		= $visitor_location['Country'];
$Country_Name 		= $visitor_location['CountryName'];
$State_Name 		= $visitor_location['RegionName'];
$City_Name 		= $visitor_location['CityName'];
$City_Latitude 		= $visitor_location['CityLatitude'];
$City_Longitude 	= $visitor_location['CityLongitude'];
$Country_Latitude 	= $visitor_location['CountryLatitude'];
$Country_Longitude 	= $visitor_location['CountryLongitude'];
$Country_Longitude 	= $visitor_location['CountryLongitude'];
$LocalTimeZone 		= $visitor_location['LocalTimeZone'];
$Calling_Code 		= $visitor_location['CallingCode'];
$Population 		= $visitor_location['Population'];
$Area_SqKm 		= $visitor_location['AreaSqKm'];
$Capital 		= $visitor_location['Capital'];
$Electrical 		= $visitor_location['Electrical'];
$Languages 		= $visitor_location['Languages'];
$Currency 		= $visitor_location['Currency'];
$Flag 			= $visitor_location['Currency'];


Download Code Total Downloads: 19153

Comments
26 February
2014
subhash
hi sir i tried this and upload in server but the city and state is not showing
the page is opening and showing like this plz tell me wht to do to find State and city
IP Address: 49.207.163.171
Country: India
State: Unknown
city: Unknown
26 February
2014
Joe Almanzo
here the result after i host this code, see the only the country is correct
IP Address: 103.23.34.16
Country: Indonesia
State: Unknown
City: Unknown

26 February
2014
Joe Almanzo
it doesnt work, city and state remains unkown :(((
22 February
2014
Shailendra
Thank you very much...!

Working Great!
19 February
2014
singh
hello i am just checking
30 January
2014
mohit Gautam
[IP] => 125.62.125.74
[ContinentCode] => AS
[ContinentName] => Asia
[CountryCode2] => IN
[CountryCode3] => IND
[Country] => IN
[CountryName] => India
[RegionName] => Punjab
[CityName] => Ludhiana

cityname incoreect coreect name mohali
28 January
2014
navjot

how can i echo the state abbreviation?

for example:

New York = NY
California = CA
28 January
2014
navjot
hi,

how can i echo the state abbreviation?

for example:

New York = NY
California = CA
26 January
2014
Max
Awesome, thank you!!
26 January
2014
Max
Awesome, thank you!!
06 January
2014
Soutam
Its working fine for desktop or laptop. But not working perfectly in mobile. Please help.
06 January
2014
Yash Karira
The script today worked stopping and is returning nothing?
Any ideas?
21 December
2013
Easytime
Hi, thanks for your wonderful helping code.
It's really nice.... It's simple to incorporate in php coding.
However, I have somewhat bugs in it. Can you please help me with why this error occurred?
There is a returned warning error message (pls see error message below).
Meanwhile, isn't there any ways one can get the api without having to connect through your server?
Thank you!


Error Message Returned!
=======================
Warning: file_get_contents(http://api.codehelper.io/ips/?php&ip=80.89.187.40): failed to open stream: Permission denied in /home/www/webaddress.com/folder/visitorlocation/userip/ip.codehelper.io.php on line 45
IP Address:
Country:
State:
City:
18 December
2013
Riaz Hussain
Hi,

I am getting below error and couldn't understand what is this. Could you help me please?

Warning: xcache_get() [function.xcache-get]: XCache var cache was not initialized properly. Check php log for actual reason in /home/mtgforex/public_html/visitor/userip/php_fast_cache.php on line 1115

Warning: xcache_set() [function.xcache-set]: XCache var cache was not initialized properly. Check php log for actual reason in /home/mtgforex/public_html/visitor/userip/php_fast_cache.php on line 1106

Thanks,

Riaz
17 December
2013
Resh
Hi

I am getting following error when i tried code online

can you please help with this?

Sorry, Please create D:\Hosting\11767128\html\LocationFinder_Ip\userip/cache.storage/ and SET Mode 0777 or any Writable Permission!


Thanks.
12 December
2013
andi flores
hi,

how can i echo the state abbreviation?

for example:

New York = NY
California = CA
04 December
2013
Yogini Barde
I get following warning and do not get any info
pl help

Warning: file_get_contents(http://api.codehelper.io/ips/?php&ip=121.245.150.233) [function.file-get-contents]: failed to open stream: Permission denied in /home/www/privatehospitalswatch.org/new/userip/ip.codehelper.io.php on line 45
IP Address:
Country:
State:
City:

04 December
2013
Rapunzel
Yesterday your script worked perfectly, today I recieve 'Unknown'.:(
($guest_country, $guest_city and $guest_state)
Any idea?

Thnx!
03 December
2013
Soutam Saha
Its working fine for desktop or laptop. But not working perfectly in mobile. Please help.
03 December
2013
luke
Hello,
is it possible to send theses datas by email?
02 December
2013
Tushar
very good code
Thanks..
14 October
2013
Akash dutta
This code is have lots of error up to line 22-43.
15 October
2013
Admin
Error Fixed.
Thanks
comments powered by Disqus