Defining HTML5 geolocation support using PHP

I need to define HTML5 support for Geolocation using PHP so that I can load fallback JavaScript that supports Geolocation using an IP address.

How to do this with or without PHP.

+4
source share
5 answers

Have you considered using Modernizr to detect HTML5 support? This is not specific to PHP, as it is done in JavaScript, but you can use this snippet to load the backup file:

if (Modernizr.geolocation){ // Access using HTML5 navigator.geolocation.getCurrentPosition(function(position) { ... }); }else{ // Load backup file var script = document.createElement('script'); script.src = '/path/to/your/script.js'; script.type = 'text/javascript'; var head = document.getElementsByTagName("head")[0]; head.appendChild(script); } 

(Based on http://www.modernizr.com/docs/#geolocation )

+7
source

You can check Geolocation support using javascript:

 function supports_geolocation() { return !!navigator.geolocation; } 

I took the function from a nice Immersion in HTML 5 .

0
source

Well, the only way to detect geolocation is with a navigator and use it like this:

 if(navigator.geolocation) { //Geo in Browser } 

So, what would I personally do is create an Ajax request to the server and do the redirection as follows:

 if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position){ /* * Send Position to server where you can store it in Session */ document.location = '/'; //Redirect and use the session data from above }, function(message){ /* * Send false to the server, and then refresh to remove the js geo check. */ }); } 

on the server side you will do something like this:

 <?php /* session/geolocation.php */ //Require system files include '../MasterBootloader.php'; /* * Session is already started in the above inclustion */ if(!isset($_SESSION['geo']['checked']) && is_agax_request()) { $_SESSION['geo'] = array(); if(isset($_GET['postition'])) { $_SESSION['geo']['supported'] = true; $_SESSION['geo']['location'] = json_decode($_REQUEST['geo_position']); } $_SESSION['geo']['checked'] = true; } ?> 

now that javascript redirects you, in your index you can check if it exists before the output of your html, then you will know the server side if GEO is supported!

0
source

You can use canisuse.js script to determine if your browsers support geolocation or not.

 caniuse.geolocation() 
0
source

All Articles