用于检测浏览器的php

时间:2013-03-14 17:15:34

标签: php cross-browser browser-detection php-5.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,与Gecko一样)Version / 6.0.2 Safari / 536.26.17

和chrome

  

Mozilla / 5.0(Macintosh; Intel 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用户代理?

8 个答案:

答案 0 :(得分: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"; }

答案 1 :(得分: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>';
}

答案 2 :(得分:0)

我使用以下代码解决了

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

答案 3 :(得分: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
        );
    } 

答案 4 :(得分: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();"添加到正文标记

  1. 放置<p id="warning"></p>以获取特殊消息

  2. id提供给您需要测试或重新设置的元素

答案 5 :(得分: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

答案 6 :(得分:-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';
}

答案 7 :(得分:-1)

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

$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";