Check visitor’s location using PHP

PHP Website

Want to get your website visitor’s location? To accomplish this, you may use cURL function which is a pre-defined PHP library.

Check out the following PHP codes:

$curl = $_SERVER['REMOTE_ADDR'];
$apiURL = 'https://freegeoip.app/json/'.$curl;
$curlObject = curl_init($apiURL);
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObject, CURLOPT_FOLLOWLOCATION, true);

// Running Session CURL
//$curlResponse = @curl_exec($curlObject);
$curlResponse = curl_exec($curlObject);
curl_close($curlObject);

// If successful execution
if($curlResponse!=false)
{
    $ipDetails   = json_decode($curlResponse, true);
    
    $countryCode = $ipDetails['country_code'];
    $countryName = $ipDetails['country_name'];
    $regionCode = $ipDetails['region_code'];
    $regionName = $ipDetails['region_name'];
    $city = $ipDetails['city'];
    $zipCode = $ipDetails['zip_code'];
    $latitude = $ipDetails['latitude'];
    $longitude = $ipDetails['longitude'];
    $timeZone = $ipDetails['time_zone'];
}
else echo "Failed!";