PHP判斷當前使用的是什么瀏覽器(推薦)
PHP簡單判斷當前使用的是什么瀏覽器,判斷瀏覽器類型的方法,方便統(tǒng)計網站訪問用戶使用瀏覽器的比例。

判斷瀏覽器類型方法一
function userBrowser() {
$user_OSagent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
$visitor_browser = "Maxthon(Microsoft IE)";
} elseif (strpos($user_OSagent, "Maxthon 2.0")) {
$visitor_browser = "Maxthon 2.0";
} elseif (strpos($user_OSagent, "Maxthon")) {
$visitor_browser = "Maxthon";
} elseif (strpos($user_OSagent, "MSIE 9.0")) {
$visitor_browser = "MSIE 9.0";
} elseif (strpos($user_OSagent, "MSIE 8.0")) {
$visitor_browser = "MSIE 8.0";
} elseif (strpos($user_OSagent, "MSIE 7.0")) {
$visitor_browser = "MSIE 7.0";
} elseif (strpos($user_OSagent, "MSIE 6.0")) {
$visitor_browser = "MSIE 6.0";
} elseif (strpos($user_OSagent, "MSIE 5.5")) {
$visitor_browser = "MSIE 5.5";
} elseif (strpos($user_OSagent, "MSIE 5.0")) {
$visitor_browser = "MSIE 5.0";
} elseif (strpos($user_OSagent, "MSIE 4.01")) {
$visitor_browser = "MSIE 4.01";
} elseif (strpos($user_OSagent, "MSIE")) {
$visitor_browser = "MSIE 較高版本";
} elseif (strpos($user_OSagent, "NetCaptor")) {
$visitor_browser = "NetCaptor";
} elseif (strpos($user_OSagent, "Netscape")) {
$visitor_browser = "Netscape";
} elseif (strpos($user_OSagent, "Chrome")) {
$visitor_browser = "Chrome";
} elseif (strpos($user_OSagent, "Lynx")) {
$visitor_browser = "Lynx";
} elseif (strpos($user_OSagent, "Opera")) {
$visitor_browser = "Opera";
} elseif (strpos($user_OSagent, "Konqueror")) {
$visitor_browser = "Konqueror";
} elseif (strpos($user_OSagent, "Mozilla/5.0")) {
$visitor_browser = "Mozilla";
} elseif (strpos($user_OSagent, "Firefox")) {
$visitor_browser = "Firefox";
} elseif (strpos($user_OSagent, "U")) {
$visitor_browser = "Firefox";
} else {
$visitor_browser = "其它";
}
return $visitor_browser;
}
方法二
下面這個是php通過正則匹配的,理論上效率不如上面的方法一, 大家可以根據(jù)需要使用。
private function getBrowser(){
$flag=$_SERVER['HTTP_USER_AGENT'];
$para=array();
// 檢查操作系統(tǒng)
if(preg_match('/Windows[\d\. \w]*/',$flag, $match)) $para['os']=$match[0];
if(preg_match('/Chrome\/[\d\.\w]*/',$flag, $match)){
// 檢查Chrome
$para['browser']=$match[0];
}elseif(preg_match('/Safari\/[\d\.\w]*/',$flag, $match)){
// 檢查Safari
$para['browser']=$match[0];
}elseif(preg_match('/MSIE [\d\.\w]*/',$flag, $match)){
// IE
$para['browser']=$match[0];
}elseif(preg_match('/Opera\/[\d\.\w]*/',$flag, $match)){
// opera
$para['browser']=$match[0];
}elseif(preg_match('/Firefox\/[\d\.\w]*/',$flag, $match)){
// Firefox
$para['browser']=$match[0];
}elseif(preg_match('/OmniWeb\/(v*)([^\s|;]+)/i',$flag, $match)){
//OmniWeb
$para['browser']=$match[2];
}elseif(preg_match('/Netscape([\d]*)\/([^\s]+)/i',$flag, $match)){
//Netscape
$para['browser']=$match[2];
}elseif(preg_match('/Lynx\/([^\s]+)/i',$flag, $match)){
//Lynx
$para['browser']=$match[1];
}elseif(preg_match('/360SE/i',$flag, $match)){
//360SE
$para['browser']='360安全瀏覽器';
}elseif(preg_match('/SE 2.x/i',$flag, $match)) {
//搜狗
$para['browser']='搜狗瀏覽器';
}else{
$para['browser']='unkown';
}
return $para;
}
保存$para即可
php判斷瀏覽器是不是IE
1、$_SERVER['HTTP_USER_AGENT']和strpos
2、打印結果
谷歌:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"
火狐:
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
IE:
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
3、php控制器中
網上說用判斷ua中是否有MSIE,然而并沒有,用Triden判斷也可以的。
public function isIE() {
$isIE = strpos($_SERVER['HTTP_USER_AGENT'],"Triden");
return $isIE;
}
總結
以上所述是小編給大家介紹的PHP判斷當前使用的是什么瀏覽器(推薦),希望對大家有所幫助!
相關文章
php reset() 函數(shù)指針指向數(shù)組中的第一個元素并輸出實例代碼
在php中,當我們使用next函數(shù)或end函數(shù)將數(shù)組內部針指指向數(shù)組最后一個元素的時候,如果再需要將內部指針指向第一個元素,可以使用reset() 函數(shù)來實現(xiàn),本文章向大家講解reset() 函數(shù)的基本語法及使用實例,需要的朋友可以參考下2016-11-11
發(fā)款php蜘蛛統(tǒng)計插件只要有mysql就可用
有時候我們?yōu)榱丝匆幌轮┲肱佬械那闆r,不得不對日志進行大量的分析,由此想做一款插件可以記錄蜘蛛的情況。在第一次做的時候,只是記錄下蜘蛛的爬行次數(shù),不大好分析。2010-10-10
PHP設計模式之策略模式(Strategy)入門與應用案例詳解
這篇文章主要介紹了PHP設計模式之策略模式(Strategy)入門與應用,結合具體實例形式詳細分析了PHP策略模式的相關概念、原理、使用方法及操作注意事項,需要的朋友可以參考下2019-12-12

