2

我有一个奇怪的 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"];,这就是我从 safari 得到的

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

并来自铬

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22

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

4

8 回答 8

8

这是基于 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"; }
于 2013-06-12T16:13:25.947 回答
0

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

$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";
于 2014-01-10T09:25:55.183 回答
0

这个请求有很多开源项目我建议将该类添加到您的 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>';
}
于 2014-01-10T14:13:46.383 回答
0

我使用以下代码解决了

$u_agent = $_SERVER['HTTP_USER_AGENT'];
$issafari=false;
if (preg_match('/Safari/i',$u_agent)){
    $issafari=(!preg_match('/Chrome/i',$u_agent));
}
于 2015-07-14T04:35:00.160 回答
0

它工作正常。

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
        );
    } 
于 2015-07-14T06:35:18.453 回答
0

我在 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. 添加 onLoad="check();"到 body 标签

  1. <p id="warning"></p>特别留言的地方

  2. Give id's 给您需要测试或重新设计的元素

于 2015-11-29T03:36:34.677 回答
0

使用这个绝对有效,我已经测试过了---------

<?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

于 2016-04-20T09:49:46.027 回答
-2

Try specific versions of Safari as well? I'm not on a Mac at the moment, so can't test this for you, but let me know if it helps:

$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';
}
于 2013-03-14T17:19:12.070 回答