2010-02-04 80 views
5

如何使用PHP獲取瀏覽器名稱?我認爲這將是直截了當的?我需要做的就是區分IE和Firefox。PHP:獲取瀏覽器名稱

+0

儘管完全可以爲瀏覽器獲取一個值(如下面的答案中所述),但您應該知道該值不一定準確。 – dnagirl 2010-02-04 13:18:01

+0

**重複的http://stackoverflow.com/questions/1895727/how-can-i-detect-the-browser-with-php-or-javascript/** – 2016-05-16 07:02:00

回答

16
if (strpos($_SERVER['HTTP_USER_AGENT'], '(compatible; MSIE ')!==FALSE) { 
    ...ie specific... 
} 

但是! 不要!

很少有理由在服務器端嗅探用戶代理。它帶來了一堆問題,其中包括:

  • 瀏覽器和撒謊,他們是誰,或者完全剝離的用戶代理頭,或者使人們普遍難以區分真正的瀏覽器是什麼其他的用戶代理來自標題文本。例如,上述規則還會檢測Opera在欺騙IE和IEMobile(Windows Mobile)時的情況,您可能會也可能不會想要它,因爲它是與桌面IE完全不同的瀏覽器。

  • 如果您在服務器端的用戶代理上進行區分,則必須在響應中返回一個Vary: User-Agent標頭,否則代理可能會緩存該頁的一個版本並將其返回給其他不匹配的瀏覽器。但是,包含這個頭文件在IE中會產生混淆緩存的副作用。

取決於它是什麼,你正在努力實現的,幾乎總是處理IE和其他瀏覽器之間的差異在客戶端,使用CSS黑客,JScript或conditional comments一個更好的方法。在你的情況下試圖檢測IE的真正目的是什麼?

+0

沒錯。如果你與; 'CURLOPT_USERAGENT('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');'和poof它的欺騙:) – 2013-03-26 12:48:05

7

$ _ SERVER [ 'HTTP_USER_AGENT']

7
<?php 
var_dump($_SERVER['HTTP_USER_AGENT']); 
var_dump(get_browser(null, true)); 
?> 

打印:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3 

Array 
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$ 
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* 
    [parent] => Firefox 0.9 
    [platform] => WinXP 
    [browser] => Firefox 
    [version] => 0.9 
    [majorver] => 0 
    [minorver] => 9 
    [cssversion] => 2 
    [frames] => 1 
    [iframes] => 1 
    [tables] => 1 
    [cookies] => 1 
    [backgroundsounds] => 
    [vbscript] => 
    [javascript] => 1 
    [javaapplets] => 1 
    [activexcontrols] => 
    [cdf] => 
    [aol] => 
    [beta] => 1 
    [win16] => 
    [crawler] => 
    [stripper] => 
    [wap] => 
    [netclr] => 
) 
1

1)精確檢測器:BrowserDetection.phpExamples

2)原語功能:

function get_user_browser() 
{ 
    $u_agent = $_SERVER['HTTP_USER_AGENT'];  $ub = ''; 
    if(preg_match('/MSIE/i',$u_agent))   { $ub = "ie";  } 
    elseif(preg_match('/Firefox/i',$u_agent)) { $ub = "firefox"; } 
    elseif(preg_match('/Safari/i',$u_agent)) { $ub = "safari"; } 
    elseif(preg_match('/Chrome/i',$u_agent)) { $ub = "chrome"; } 
    elseif(preg_match('/Flock/i',$u_agent)) { $ub = "flock";  } 
    elseif(preg_match('/Opera/i',$u_agent)) { $ub = "opera";  } 
    return $ub; 
} 
+0

謝謝工作完美! – 2016-06-29 10:30:34

8

嘗試此代碼...

<?php 
function getBrowser() 
{ 
    $u_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 
    ); 
} 

// now try it 
$ua=getBrowser(); 
$yourbrowser= "Your browser: " . $ua['name']; 
echo $yourbrowser; 

?> 


火狐

的輸出
Mozilla Firefox