Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Geolocation

Geolocation is the process of determining the real-world physical location of a device, a user, or a piece of content. On the web, a browser can report a device’s coordinates directly through the HTML5 Geolocation API, using GPS, Wi-Fi positioning, or cell-tower triangulation, and a PHP backend then simply receives and stores the latitude and longitude sent by client-side JavaScript. When no device-reported coordinates are available, an approximate location can instead be inferred server-side from the visitor’s IP address, a technique known as GeoIP. Typical uses in applications include localizing content and currency, restricting or redirecting access by country, pre-filling shipping addresses, powering store-locator features, and enriching analytics or fraud-detection signals with a rough location.

<?php

    // Coordinates sent by the browser's navigator.geolocation API as POST fields
    $latitude  = (float) ($_POST['latitude']  ?? 0);
    $longitude = (float) ($_POST['longitude'] ?? 0);
    
    if ($latitude !== 0.0 && $longitude !== 0.0) {
        echo "User is near {$latitude}, {$longitude}";
    }

?>

Documentation

See Also