本文实例讲述了php获取用户浏览器版本的方法。分享给大家供大家参考。具体分析如下:
在php中我们有个全局变量$_SERVER['HTTP_USER_AGENT'];可以获取用户所有信息,我们要经过处理才可以判断用户浏览器是哪种类型的,下面的函数就可以准确的用户浏览器版本号代码.
复制代码 代码如下:
function getbrowse()
{
$agent = $_server['http_user_agent'];
$browser = '';
$browserver = '';
$browser = array('lynx', 'mosaic', 'aol', 'opera', 'java', 'macweb', 'webexplorer', 'omniweb');
for($i = 0; $i <= 7; $i ++){
if(strpos($agent, $browsers[$i])){
$browser = $browsers[$i];
$browserver = '';
}
}
if(ereg('mozilla', $agent) && !ereg('msie', $agent)){
$temp = explode('(', $agent);
$part = $temp[0];
$temp = explode('/', $part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'netscape navigator';
}
if(ereg('mozilla', $agent) && ereg('opera', $agent)) {
$temp = explode('(', $agent);
$part = $temp[1];
$temp = explode(')', $part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'opera';
}
if(ereg('mozilla', $agent) && ereg('msie', $agent)){
$temp = explode('(', $agent);
$part = $temp[1];
$temp = explode(';', $part);
$part = $temp[1];
$temp = explode(' ', $part);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/','\1',$browserver);
$browserver = $browserver;
$browser = 'internet explorer';
}
if($browser != ''){
$browseinfo = $browser.' '.$browserver;
} else {
$browseinfo = false;
}
return $browseinfo;
}
//应用方法
{
$agent = $_server['http_user_agent'];
$browser = '';
$browserver = '';
$browser = array('lynx', 'mosaic', 'aol', 'opera', 'java', 'macweb', 'webexplorer', 'omniweb');
for($i = 0; $i <= 7; $i ++){
if(strpos($agent, $browsers[$i])){
$browser = $browsers[$i];
$browserver = '';
}
}
if(ereg('mozilla', $agent) && !ereg('msie', $agent)){
$temp = explode('(', $agent);
$part = $temp[0];
$temp = explode('/', $part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'netscape navigator';
}
if(ereg('mozilla', $agent) && ereg('opera', $agent)) {
$temp = explode('(', $agent);
$part = $temp[1];
$temp = explode(')', $part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'opera';
}
if(ereg('mozilla', $agent) && ereg('msie', $agent)){
$temp = explode('(', $agent);
$part = $temp[1];
$temp = explode(';', $part);
$part = $temp[1];
$temp = explode(' ', $part);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/','\1',$browserver);
$browserver = $browserver;
$browser = 'internet explorer';
}
if($browser != ''){
$browseinfo = $browser.' '.$browserver;
} else {
$browseinfo = false;
}
return $browseinfo;
}
//应用方法
//在ie中
echo getbrowse(); //internet explorer 6.0
//在firefox中
echo getbrowse() ;//netscape navigator 5.0
希望本文所述对大家的php程序设计有所帮助。