get_browser()在Chrome的回报“默认浏览器” [英] get_browser() in Chrome returns 'Default Browser'
本文介绍了get_browser()在Chrome的回报“默认浏览器”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经得到了我使用来检测用户的浏览器之前,我显示我的网页给他一个PHP脚本。
使用get_browser()方法,并返回数组索引数组['浏览器']',从镀铬的窗口,返回默认浏览器。我不知道是否有人遇到这样或类似的情况。我敢肯定是这样的话。
感谢任何人的帮助。
解决方案
< PHP
功能getBrowser()
{
$ u_agent = $ _ SERVER ['HTTP_USER_AGENT'];
$ BNAME ='未知';
$平台='未知';
$版=; //首先得到该平台?
如果(preg_match('/ Linux的/ I',$ u_agent)){
$平台='Linux的;
}
ELSEIF(preg_match('/麦金塔|的Mac OS X / I',$ u_agent)){
$平台='MAC';
}
ELSEIF(preg_match('/窗|的Win32 / I',$ u_agent)){
$平台='窗口';
} //然后获得用户代理的名称是单独有很好的理由
如果(preg_match('/ MSIE / I',$ u_agent)及和放大器;!preg_match('/歌剧/ I',$ u_agent))
{
$ BNAME ='Internet Explorer的;
$ UB =MSIE
}
ELSEIF(preg_match('/火狐/ I',$ u_agent))
{
$ BNAME ='Mozilla Firefox浏览器';
$ UB =火狐;
}
ELSEIF(preg_match('/镀铬/ I',$ u_agent))
{
$ BNAME ='谷歌Chrome的;
$ UB =铬;
}
ELSEIF(preg_match('/ Safari浏览器/ I',$ u_agent))
{
$ BNAME ='苹果Safari浏览器';
$ UB =野生动物园;
}
ELSEIF(preg_match('/歌剧/ I',$ u_agent))
{
$ BNAME ='歌剧';
$ UB =戏;
}
ELSEIF(preg_match('/网景公司/ I',$ u_agent))
{
$ BNAME ='Netscape的;
$ UB =网景;
} //最终得到正确的版本号
$称为=阵列('版本',$ UB,其他);
。$模式='?#(小于浏览器>'加入('|',$已知的)。
')[/] +(小于版本> [0-9 | A-ZA-Z] *)#';
如果(!preg_match_all($模式,$ u_agent,$匹配)){
//我们没有匹配的数字只是继续
} //看看我们有多少
$ I = COUNT($比赛['浏览器']);
如果($ I!= 1){
//因为我们不使用'其他'的说法而我们将有两个
//是否版本之前或后名
如果(strripos($ u_agent,版本)< strripos($ u_agent,$ UB)){
$版= $比赛['版本'] [0];
}
其他{
$版= $比赛['版本'] [1];
}
}
其他{
$版= $比赛['版本'] [0];
} //检查是否我们有一些
如果($版本== NULL || $版本==){$版=;}? 返回阵列(
'的userAgent'=> $ u_agent,
'名'=> $ BNAME,
'版本'=> $版本,
平台=> $平台,
'模式'=> $模式
);
}//现在试试吧
$ UA = getBrowser();
$ yourbrowser =您的浏览器。 $ UA ['名']。 。 $ UA ['版本'。 关于。$ UA ['平台']。 报告:其中; BR>中。 $ UA ['的userAgent'];
的print_r($ yourbrowser);
?>
来源: http://www.php.net/手动/ EN / function.get-browser.php#101125
I've got a PHP script that I'm using to detect a users browser before I display my page to him.
Using the get_browser() method, and the returned array index 'array['browser']', from a chrome window, returns 'Default Browser'. I wonder if anyone has encountered this, or a similar situation. I'm sure that's the case.
Thanks for anyone's help.
解决方案
<?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'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];
print_r($yourbrowser);
?>
Source: http://www.php.net/manual/en/function.get-browser.php#101125
这篇关于get_browser()在Chrome的回报“默认浏览器”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文