Last Updated:
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.
<?php
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
echo $get_client_ip;
?>
<?php
$PublicIP = get_client_ip();
$json = file_get_contents('https://api.snoopi.io/$PublicIP?apikey=[YOUR API KEY]');
$json = json_decode($json ,true);
?>
$CountryCode = $json['CountryCode']; $CountryName = $json['CountryName']; $GeoID = $json['GeoID']; $State = $json['State']; $StateCode = $json['StateCode']; $City = $json['City']; $Postal = $json['Postal']; $Latitude = $json['Latitude']; $Longitude = $json['Longitude']; $RequestTime = $json['RequestTime']; $RequestedIP = $json['RequestedIP'];
CaliforniaCALos Angeles9000134.052231-118.2436831773748850172.64.217.39
<?php
$PublicIP = get_client_ip();
$json = file_get_contents('http://api.ipstack.com/$PublicIP?access_key=[YOUR API KEY]');
$json = json_decode($json ,true);
?>
$continent_name = $json['continent_name']; $country_code = $json['country_code']; $country_name = $json['country_name']; $region_code = $json['region_code']; $region_name = $json['region_name']; $city = $json['city']; $zip = $json['zip']; $latitude = $json['latitude']; $longitude = $json['longitude'];
North AmericaUSUnited StatesOhioOHColumbus4320139.995578765869-82.999458312988