【问题标题】:Check if PHP-page is accessed from an iOS device检查是否从 iOS 设备访问 PHP 页面
【发布时间】:2011-09-13 09:46:19
【问题描述】:

我有一个简单的 PHP 网页,希望返回不同的内容,具体取决于是从 iPhone/iPad 还是从网络浏览器访问。我该怎么做?

【问题讨论】:

    标签: php iphone ios http-headers


    【解决方案1】:

    使用来自$_SERVER['HTTP_USER_AGENT']的用户代理, 对于简单的检测,您可以使用this 脚本:

    <?php
    
    //Detect special conditions devices
    $iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
    $webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
    
    //do something with this information
    if( $iPod || $iPhone ){
        //browser reported as an iPhone/iPod touch -- do something here
    }else if($iPad){
        //browser reported as an iPad -- do something here
    }else if($Android){
        //browser reported as an Android device -- do something here
    }else if($webOS){
        //browser reported as a webOS device -- do something here
    }
    
    ?> 
    

    如果您想了解用户设备的更多详细信息,我建议使用以下解决方案之一:http://51degrees.mobihttp://deviceatlas.com

    【讨论】:

    • 但这不是真的,我发现了。这些标题只是来自手机中的 Safari。如果您从应用程序连接,它看起来像这样: app%20name/1.0 CFNetwork/485.13.8 Darwin/11.0.0 。所以也许我可以寻找 CFNetwork 或 Darwin,或者应用名称。
    • 不适合我我得到了适用于 iPhone 和 iPad 的 1.1 CFNetwork/548.0.3 Darwin/11.0.0 :S 现在我应该如何区分它们???
    • @AsadKhan 穿着你的模拟器测试?我得到相同的 Http_user_agent
    • 更新新:$iPadOS = stripos($_SERVER['HTTP_USER_AGENT'],"Macintosh");
    • 完美运行。在 2 部 iphone、Ipad 上测试。感谢它的 2020 年!!
    【解决方案2】:
    $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    

    【讨论】:

      【解决方案3】:

      它适用于 Iphone

      <?php
        $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
        if ($browser == true){
          $browser = 'iphone';
        }
      ?>
      

      【讨论】:

        【解决方案4】:
        preg_match("/iPhone|Android|iPad|iPod|webOS/", $_SERVER['HTTP_USER_AGENT'], $matches);
        $os = current($matches);
        
        switch($os){
           case 'iPhone': /*do something...*/ break;
           case 'Android': /*do something...*/ break;
           case 'iPad': /*do something...*/ break;
           case 'iPod': /*do something...*/ break;
           case 'webOS': /*do something...*/ break;
        }
        

        【讨论】:

          【解决方案5】:

          如果你只是想检测通用的移动设备,Cake 内置了使用 RequestHandler->isMobile() (http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::isMobile) 的支持

          【讨论】:

            【解决方案6】:
            function user_agent(){
                $iPod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
                $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
                $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
                $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
                file_put_contents('./public/upload/install_log/agent',$_SERVER['HTTP_USER_AGENT']);
                if($iPad||$iPhone||$iPod){
                    return 'ios';
                }else if($android){
                    return 'android';
                }else{
                    return 'pc';
                }
            }
            

            【讨论】:

              【解决方案7】:
              <?php
              $iPhone = false;
              $AndroidPhone = false;
              $deviceType = 0;
              
              $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
              print "<br>".$ua;
              
              if(strpos($ua,"iphone") !== false ){
                  $iPhone = true;
              }
              if(strpos($ua,"android") !== false){
              
                  if(strpos($_SERVER['HTTP_USER_AGENT'],"mobile")){
                      $AndroidPhone = true;        
                  }
              }
              if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){
                  $iPad = true;
                  $Tablet = true;
                  $iOS = true;
              }   
              
              if($AndroidPhone==true || $iPhone==true)
              {
                  $deviceType = 1;
              }
              ?>
              

              【讨论】:

                【解决方案8】:
                function isIosDevice(){
                    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
                    $iosDevice = array('iphone', 'ipod', 'ipad');
                    $isIos = false;
                
                    foreach ($iosDevice as $val) {
                        if(stripos($userAgent, $val) !== false){
                            $isIos = true;
                            break;
                        }
                    }
                
                    return $isIos;
                }
                

                【讨论】:

                • 您应该添加一个!empty($_SERVER['HTTP_USER_AGENT']),因为当客户端根本不发送任何用户代理时,$_SERVER['HTTP_USER_AGENT'] 未设置。然后你的函数会崩溃/显示一个通知。
                【解决方案9】:

                51Degrees 的 PHP 解决方案能够做到这一点。你可以在这里https://github.com/51Degrees/Device-Detection 获得免费的开源 API。您可以使用 HardwareFamily 属性来确定它是否是 iPad/iPod/iPhone 等。

                由于 Apple 用户代理的性质,初始结果将返回通用设备,但是如果您对特定设备感兴趣,可以使用 JavaScript 客户端覆盖来确定特定型号。

                为此,您可以在确定它是 Apple 设备(在本例中为 iPhone)后实现类似于以下逻辑的操作。

                // iPhone model checks.
                function getiPhoneModel() {
                // iPhone 6 Plus
                if ((window.screen.height / window.screen.width == 736 / 414) && 
                (window.devicePixelRatio == 3)) {
                return "iPhone 6 Plus";
                }
                // iPhone 6
                else if ((window.screen.height / window.screen.width == 667 / 375) && 
                (window.devicePixelRatio == 2)) {
                return "iPhone 6";
                }
                // iPhone 5/5C/5S or 6 in zoom mode
                else if ((window.screen.height / window.screen.width == 1.775) && 
                (window.devicePixelRatio == 2)) {
                return "iPhone 5, 5C, 5S or 6 (display zoom)";
                }
                // iPhone 4/4S
                else if ((window.screen.height / window.screen.width == 1.5) && 
                (window.devicePixelRatio == 2)) {
                return "iPhone 4 or 4S";
                }
                // iPhone 1/3G/3GS
                else if ((window.screen.height / window.screen.width == 1.5) && 
                (window.devicePixelRatio == 1)) {
                return "iPhone 1, 3G or 3GS";
                } else {
                return "Not an iPhone";
                };
                }
                

                或者对于 iPad

                function getiPadVersion() {
                var pixelRatio = getPixelRatio();
                var return_string = "Not an iPad";
                if (pixelRatio == 1 ) {
                return_string = "iPad 1, iPad 2, iPad Mini 1";
                }
                if (pixelRatio == 2) {
                return_string = "iPad 3, iPad 4, iPad Air 1, iPad Air 2, iPad Mini 2, iPad 
                Mini 3";
                }
                return return_string;
                }
                

                有关 51Degrees 对 Apple 设备进行的研究的更多信息,您可以在此处阅读他们的博客文章 https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad

                披露:我为 51Degrees 工作。

                【讨论】:

                  【解决方案10】:

                  为了响应 Haim Evgi 的代码,我在末尾添加了 !== false 以便它为我工作

                  $iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod") !== false;
                  $iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone") !== false;
                  $iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad") !== false;
                  $Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android") !== false;
                  

                  【讨论】:

                  • 最好将此作为评论添加到他的回答中,尽管我知道您已经有一段时间没有发表评论了。感谢您的代码!
                  • @BrianC 叹了口气,Stack Overflow 说我必须有 50 个声望才能发表评论。
                  猜你喜欢
                  • 2011-07-03
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-07-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-01-15
                  相关资源
                  最近更新 更多