Get users general location without services
by kahlil | Feb 27, 2023 | Featured
<?php
$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';
$url = "https://json.geoiplookup.io/" . $ipaddress . "";
$json = file_get_contents($url);
$obj = json_decode($json, TRUE);
$country_name = '';
foreach ($obj as $key => $value) {
// echo 'Your key is: '.$key.' and the value of the key is:'.$value;
if ($key == 'country_name') {
$country_name = $value;
}
}
?>