【问题标题】:php to detect browserphp检测浏览器
【发布时间】:2013-03-03 04:36:51
【问题描述】:

我有一个奇怪的 css 错误,我似乎无法修复,它只发生在 safari 中,而不是 chrome,所以 webkit 目标不会有帮助.. 我试图做的是设置一个 php 块来检查浏览器是否是 safari,如果是这样,请回显一些 CSS。

到目前为止,我得到了这个(下面) - 哪个有效,但它也在 chrome 中输出 echo 语句,我有什么想法出错了吗?

<?php 

    if(isset($_SERVER['HTTP_USER_AGENT'])){
         $agent = $_SERVER['HTTP_USER_AGENT'];
    }

    if(strlen(strstr($agent,"Safari")) > 0 ){
        $browser = 'safari';
    }
    if($browser=='safari'){
        echo '<style>p {font-weight: 300;}</style>';
    }

?>

我刚刚和echo $_SERVER["HTTP_USER_AGENT"]; 一起玩arround,这就是我从safari 得到的

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML,如 Gecko)版本/6.0.2 Safari/536.26.17

来自 chrome

Mozilla/5.0(Macintosh;英特尔 Mac OS X 10_8_2)AppleWebKit/537.22 (KHTML,如 Gecko)Chrome/25.0.1364.172 Safari/537.22

所以它可以告诉它不同的浏览器,但它显然将它们都读取为 Apple Web Kit,而不是 safari 或 chrome。我的 php 有点生疏了,我该如何让它专门针对 safari 用户代理?

【问题讨论】:

标签: php cross-browser browser-detection php-5.2


【解决方案1】:

也尝试特定版本的 Safari?我目前不在 Mac 上,因此无法为您测试,但如果有帮助,请告诉我:

$ua = $_SERVER["HTTP_USER_AGENT"];

$safari = strpos($ua, 'Safari') ? true : false; // All Safari
$safari_2 = strpos($ua, 'Safari/419') ? true : false; // Safari 2
$safari_3 = strpos($ua, 'Safari/525') ? true : false; // Safari 3
$safari_3_1 = strpos($ua, 'Safari/528') ? true : false; // Safari 3.1
$safari_4 = strpos($ua, 'Safari/531') ? true : false; // Safari 4

// Test Versions
if ($safari_2) { // Safari 2
    echo 'Safari Version 2';
}
elseif ($safari_3) { // Safari 3
    echo 'Safari Version 3';
}
elseif ($safari_4) { // Safari 4
    echo 'Safari Version 4';
}

【讨论】:

  • 我添加了这一行 - if ($safari) { // Safari echo 'Safari Version all'; } - 只是在 //Test Versions 注释下方,并将 $safari_2 语句更改为 elseif,我得到了“safari version all”,如果我完全按照上面的方式运行你的代码,我什么也得不到。我认为这是因为我正在运行 safari 6.02
【解决方案2】:

这是基于 po228 回答的更通用的解决方案。我认为直接搜索浏览器的版本号比搜索版本号更容易。

$ua = $_SERVER["HTTP_USER_AGENT"];      // Get user-agent of browser

$safariorchrome = strpos($ua, 'Safari') ? true : false;     // Browser is either Safari or Chrome (since Chrome User-Agent includes the word 'Safari')
$chrome = strpos($ua, 'Chrome') ? true : false;             // Browser is Chrome

if($safariorchrome == true AND $chrome == false){ $safari = true; }     // Browser should be Safari, because there is no 'Chrome' in the User-Agent

// Check for version numbers 
$v1 = strpos($ua, 'Version/1.') ? true : false;
$v2 = strpos($ua, 'Version/2.') ? true : false;
$v3 = strpos($ua, 'Version/3.') ? true : false;
$v4 = strpos($ua, 'Version/4.') ? true : false;
$v5 = strpos($ua, 'Version/5.') ? true : false;
$v6 = strpos($ua, 'Version/6.') ? true : false;

// Test versions of Safari
if($safari AND $v1){ echo 'Safari Version 1'; }
else if($safari AND $v2){ echo 'Safari Version 2'; }
else if($safari AND $v3){ echo 'Safari Version 3'; }
else if($safari AND $v4){ echo 'Safari Version 4'; }
else if($safari AND $v5){ echo 'Safari Version 5'; }
else if($safari AND $v6){ echo 'Safari Version 6'; }
else if($safari){ echo 'Safari newer than Version 6'; }
else{ echo "Not Safari"; }

【讨论】:

  • $v1 = strpos($ua, 'Version/1.') ? true : false;
【解决方案3】:

我用这个。这并不完美,但它有效

$browser = $_SERVER['HTTP_USER_AGENT'];
$chrome = '/Chrome/';
$firefox = '/Firefox/';
$ie = '/MSIE/';
if (preg_match($chrome, $browser))
    echo "Chrome/Opera";
if (preg_match($firefox, $browser))
    echo "Firefox";
if (preg_match($ie, $browser))
    echo "Ie";

【讨论】:

  • 为什么要使用preg_match() 来进行简单的字符串搜索?
【解决方案4】:

这个请求有很多开源项目我建议将这个类添加到你的 PHP 项目中并在需要时运行它: https://github.com/cbschuld/Browser.php

但是,如果您只是想根据您的代码解决问题,我认为这样做可以:

$agent = '';
$browser = '';
if(isset($_SERVER['HTTP_USER_AGENT'])){
     $agent = $_SERVER['HTTP_USER_AGENT'];
}

if(strlen(strstr($agent,'Safari')) > 0 ){
    $browser = 'safari';
   if(strstr($agent,'Chrome')){
     $browser= 'chrome';
   }
}

if($browser=='safari'){
    echo '<style>p {font-weight: 300;}</style>';
}

【讨论】:

    【解决方案5】:

    我用下面的代码解决了

    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $issafari=false;
    if (preg_match('/Safari/i',$u_agent)){
        $issafari=(!preg_match('/Chrome/i',$u_agent));
    }
    

    【讨论】:

      【解决方案6】:

      效果很好。

      function getBrowser($agent = null){
              $u_agent = ($agent!=null)? $agent : $_SERVER['HTTP_USER_AGENT']; 
              $bname = 'Unknown';
              $platform = 'Unknown';
              $version= "";
      
              //First get the platform?
              if (preg_match('/linux/i', $u_agent)) {
                  $platform = 'linux';
              }
              elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
                  $platform = 'mac';
              }
              elseif (preg_match('/windows|win32/i', $u_agent)) {
                  $platform = 'windows';
              }
      
              // Next get the name of the useragent yes seperately and for good reason
              if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
              { 
                  $bname = 'Internet Explorer'; 
                  $ub = "MSIE"; 
              } 
              elseif(preg_match('/Firefox/i',$u_agent)) 
              { 
                  $bname = 'Mozilla Firefox'; 
                  $ub = "Firefox"; 
              } 
              elseif(preg_match('/Chrome/i',$u_agent)) 
              { 
                  $bname = 'Google Chrome'; 
                  $ub = "Chrome"; 
              } 
              elseif(preg_match('/Safari/i',$u_agent)) 
              { 
                  $bname = 'Apple Safari'; 
                  $ub = "Safari"; 
              } 
              elseif(preg_match('/Opera/i',$u_agent)) 
              { 
                  $bname = 'Opera'; 
                  $ub = "Opera"; 
              } 
              elseif(preg_match('/Netscape/i',$u_agent)) 
              { 
                  $bname = 'Netscape'; 
                  $ub = "Netscape"; 
              } 
      
              // finally get the correct version number
              $known = array('Version', $ub, 'other');
              $pattern = '#(?<browser>' . join('|', $known) .
              ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
              if (!preg_match_all($pattern, $u_agent, $matches)) {
                  // we have no matching number just continue
              }
      
              // see how many we have
              $i = count($matches['browser']);
              if ($i != 1) {
                  //we will have two since we are not using 'other' argument yet
                  //see if version is before or after the name
                  if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
                      $version= $matches['version'][0];
                  }
                  else {
                      $version= $matches['version'][1];
                  }
              }
              else {
                  $version= $matches['version'][0];
              }
      
              // check if we have a number
              if ($version==null || $version=="") {$version="?";}
      
              return array(
                  'userAgent' => $u_agent,
                  'name'      => $bname,
                  'version'   => $version,
                  'platform'  => $platform,
                  'pattern'    => $pattern
              );
          } 
      

      【讨论】:

        【解决方案7】:

        我在 Safari 上遇到了类似的问题(float: right; 拒绝坐在另一列旁边)最终使用 JavaScript 而不是问题浏览器来检测问题。

        JavaScript 代码:

        function check() { //called by HTML's <body onLoad="check();">  
          topPos = document.getElementById("rightCol").offsetTop;
          //has rightCol been placed below leftCol ??   
          if (topPos > 200) {
            //if you want a message - something like this works  
            text = "Apologies to Safari (and other?) users: the important links   are at the bottom of the page." + topPos.toString();
            document.getElementById("warning").appendChild(document.createTextNode(text));
            //or, if you want to override your preferred styles something like this  
            document.getElementById("leftCol").style.width = "100%";
            document.getElementById("rightCol").style.width = "100%";
            document.getElementById("rightCol").style.borderTop = "thin dotted black";
          } else //removes the warning paragraph (if required)  
            document.getElementById("warning").normalise();
        }
        

        在 HTML 中: 1.在body标签中添加onLoad="check();"

        1. 发送&lt;p id="warning"&gt;&lt;/p&gt; 以获取特殊信息

        2. id 分配给您需要测试或重新设置样式的元素

        【讨论】:

          【解决方案8】:

          使用这个绝对可以,我已经测试过了---------

          <?php
          $msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false;
          $firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
          $safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
          $chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
          ?>
          
          
          <?php
          //Firefox
          if ($firefox) {
          echo 'you are using Firefox!';
          echo '<br />';
          }
          
          // Safari or Chrome. Both use the same engine - webkit
          if ($safari || $chrome) {
          echo 'you are using a webkit powered browser';
          echo '<br />';
          }
          
          // IE
          if ($msie) {
          echo '<br>you are using Internet Explorer<br>';
          echo '<br />';
          }
          
          // Not IE and for all other browsers
          if (!$msie) {
          echo '<br>you are not using Internet Explorer<br>';
          echo '<br />';
          }
          
          // Add inline css
          if ($firefox) {
          echo '<style type="text/css">';
          echo '.mydiv {position:relative; width:100px; height:50px;}';
          echo '</style>';
          }
          ?>
          

          原文:-http://echopx.com/faq/browser-detection-ie-firefox-safari-chrome

          【讨论】:

            猜你喜欢
            • 2011-04-18
            • 1970-01-01
            • 2013-01-31
            • 2014-12-28
            • 2011-09-14
            • 1970-01-01
            • 2011-01-14
            • 2011-04-15
            相关资源
            最近更新 更多