【问题标题】:Detect HTML5 Geolocation Support Using PHP使用 PHP 检测 HTML5 地理位置支持
【发布时间】:2010-11-30 10:50:07
【问题描述】:

我需要使用 PHP 检测 HTML5 地理定位支持,以便我可以加载支持使用 IP 地址进行地理定位的备份 JavaScript。

如何使用或不使用 PHP。

【问题讨论】:

    标签: php html geolocation


    【解决方案1】:

    您是否考虑过使用Modernizr 来检测对HTML5 的支持?这不是 PHP 特定的,因为它是在 JavaScript 中完成的,但是您可以使用这个 sn-p 来加载您的备份文件:

    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);
    }
    

    (基于http://www.modernizr.com/docs/#geolocation)

    【讨论】:

    • 在 JavaScript 本身中创建标签的好主意。正是我需要的。谢谢!
    • 啊 - 不知道。谢谢保罗
    【解决方案2】:
    【解决方案3】:

    检测地理位置的唯一方法是使用导航器,使用方式如下:

    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!

    【讨论】:

      【解决方案4】:

      您可以使用 javascript 检查地理位置支持:

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

      我已经从漂亮的Dive into HTML 5 那里获得了这个功能。

      【讨论】:

        【解决方案5】:

        您可以使用canisuse.js 脚本来检测您的浏览器是否支持地理定位

        caniuse.geolocation()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-10
          • 2012-08-16
          • 2016-09-21
          相关资源
          最近更新 更多