Search code examples
javascriptphpgeolocation

Geolocation with PHP


Is there a way to get lat/long into PHP in a single file solution? Normally using JS I would use this in a pure JS sulution, but now I need the values in PHP variables:

<script>
$(document).ready(function(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showLocation);
    } else {
        $('#location').html('Geolocation is not supported by this browser.');
    }
});
function showLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    return latitude;
}
</script>

I need to get the lat/long vars into PHP without using an AJAX post into a url: xxx.php type of sulution. Single file only. Any ideas on a clean way to execute this anyone?

BTW- If I write them into cookies this only works if I reload the doc. :-(

Thanks!


Solution

  • You can use geoplugin api Link

    <?php
      echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp? 
      ip='.$_SERVER['REMOTE_ADDR'])));
    ?>