php php检测浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15415883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php to detect browser
提问by sam
Ive got an odd css fault that i cant seem to fix, its only occuring in safari, not chrome so webkit targeting wont help.. what i was trying to do is set a block of php to check if the browsers safari, if so echo a peice of css.
我有一个奇怪的 css 错误,我似乎无法修复,它只发生在 safari 中,而不是 chrome 中,所以 webkit 定位无济于事。一个css。
So far ive got this (bellow) - Which works, but its also outputting the echo statement in chrome, any idea were ive gone wrong ?
到目前为止,我得到了这个(波纹管) - 哪个有效,但它也在 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>';
}
?>
Ive just been playing arround with echo $_SERVER["HTTP_USER_AGENT"];and this is what i get from safari
我一直在玩echo $_SERVER["HTTP_USER_AGENT"];,这就是我从 safari 中得到的
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17
and from chrome
和铬
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
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
So it can tell its different browsers, but its obviously reading them both as Apple Web Kit, rather than safari or chrome. My php is a bit rusty how would i get it to target the safari user agent specificly ?
所以它可以告诉它不同的浏览器,但它显然将它们都读为 Apple Web Kit,而不是 safari 或 chrome。我的 php 有点生疏,我如何让它专门针对 safari 用户代理?
回答by wicketyjarjar
Here is a more general solution based on po228's answer. I think it is easier to search directly for the version number of the browser, rather than the build number.
这是基于 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"; }
回答by WebArtisan
I use this. this is not perfect, but it works
我用这个。这并不完美,但它有效
$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";
回答by talsibony
There are many open source projects for this request I suggest to add the class to your PHP project and run it when needed: https://github.com/cbschuld/Browser.php
这个请求有很多开源项目我建议把这个类添加到你的 PHP 项目中并在需要时运行它:https: //github.com/cbschuld/Browser.php
but if you just want to solve your issue acourding to your code I think this will do it:
但如果你只是想根据你的代码解决你的问题,我认为这会做到:
$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>';
}
回答by Natura Ilec
i solved using the following code
我使用以下代码解决了
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$issafari=false;
if (preg_match('/Safari/i',$u_agent)){
$issafari=(!preg_match('/Chrome/i',$u_agent));
}
回答by Ng? V?n Thao
It works fine.
它工作正常。
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
);
}
回答by RobB
I had a similar issue with Safari (a float: right;refusing to sit alongside another column) ended up using JavaScript to detect the problem rather than the problem browser.
我在 Safari 上遇到了类似的问题(float: right;拒绝坐在另一列旁边)最终使用 JavaScript 来检测问题,而不是浏览器的问题。
JavaScript code:
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();
}
In Html:
1. Add onLoad="check();"to the body tag
在 Html 中: 1. 添加 onLoad="check();"到 body 标签
Place
<p id="warning"></p>for a special messageGive
id's to the elements you need to test or restyle
放置
<p id="warning"></p>一个特殊的消息给
id你需要测试或重新设计的元素
回答by gopal sharma
Use This one definitly work, I already tested it---------
使用这个绝对有效,我已经测试过了---------
<?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>';
}
?>
Original:- http://echopx.com/faq/browser-detection-ie-firefox-safari-chrome
原文:- http://echopx.com/faq/browser-detection-ie-firefox-safari-chrome
回答by po228
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:
也尝试特定版本的 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';
}

