使用PHP检测HTML5地理位置支持
我需要使用PHP检测HTML5地理位置支持,这样我就可以加载支持使用IP地址地理位置的备份JavaScript使用PHP检测HTML5地理位置支持,php,html,geolocation,Php,Html,Geolocation,我需要使用PHP检测HTML5地理位置支持,这样我就可以加载支持使用IP地址地理位置的备份JavaScript 如何在使用或不使用PHP的情况下执行此操作。您可以使用javascript检查地理位置支持: function supports_geolocation() { return !!navigator.geolocation; } 我从nice获取了这个函数。您考虑过使用它来检测HTML5支持吗?这不是特定于PHP的,因为它是在JavaScript中完成的,但您可以使用此
如何在使用或不使用PHP的情况下执行此操作。您可以使用javascript检查地理位置支持:
function supports_geolocation() {
return !!navigator.geolocation;
}
我从nice获取了这个函数。您考虑过使用它来检测HTML5支持吗?这不是特定于PHP的,因为它是在JavaScript中完成的,但您可以使用此代码段加载备份文件:
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);
}
(基于)检测地理位置的唯一方法是使用导航器,如下所示:
if(navigator.geolocation)
{
//Geo in Browser
}
所以我个人要做的是创建一个到服务器的Ajax请求,并执行如下重定向:
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.
*/
});
}
在服务器端,您可以这样做:
<?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;
}
?>
现在,当javascript重定向您时,在您的索引中,您可以在输出html之前检查是否存在,然后您将知道服务器端是否支持GEO 没有PHP:
使用PHP:
您必须检查浏览器版本
(注意:)您可以使用脚本来检测浏览器是否支持地理定位
caniuse.geolocation()
在JavaScript本身中创建标记是个好主意。正是我需要的。谢谢啊,我不知道。谢谢你,保罗