当前位置:Gxlcms > PHP教程 > php怎么获取浏览器的类型?

php怎么获取浏览器的类型?

时间:2021-07-01 10:21:17 帮助过:22人阅读

php 怎么获取访问访问页面的浏览器类型,包括客户端和浏览器类型

回复内容:

php 怎么获取访问访问页面的浏览器类型,包括客户端和浏览器类型

github上有现成的开源项目可以参考下:
比如:cbschuld的这个Browser项目,支持识别以下浏览器:

Opera Browser::BROWSER_OPERA
WebTV Browser::BROWSER_WEBTV NetPositive Browser::BROWSER_NETPOSITIVE
Internet Explorer Browser::BROWSER_IE Pocket Internet Explorer
Browser::BROWSER_POCKET_IE Galeon Browser::BROWSER_GALEON Konqueror
Browser::BROWSER_KONQUEROR iCab Browser::BROWSER_ICAB OmniWeb
Browser::BROWSER_OMNIWEB Phoenix Browser::BROWSER_PHOENIX Firebird
Browser::BROWSER_FIREBIRD Firefox Browser::BROWSER_FIREFOX Mozilla
Browser::BROWSER_MOZILLA Amaya Browser::BROWSER_AMAYA Lynx
Browser::BROWSER_LYNX Safari Browser::BROWSER_SAFARI iPhone
Browser::BROWSER_IPHONE iPod Browser::BROWSER_IPOD Google’s Android
Browser::BROWSER_ANDROID Google’s Chrome Browser::BROWSER_CHROME
GoogleBot Browser::BROWSER_GOOGLEBOT Yahoo!’s Slurp
Browser::BROWSER_SLURP W3C’s Validator Browser::BROWSER_W3CVALIDATOR
BlackBerry Browser::BROWSER_BLACKBERRY

print_r($_SERVER['HTTP_USER_AGENT']);

php中有一个$_SERVER['HTTP_USER_AGENT']能获取到浏览器和操作系统的信息,如在我自己的电商上输出的是:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36

从这个里面就能分析出当前用户使用的浏览器内核、浏览器版本和操作系统等信息

区分移动设备和pc访问站点

可以试下 php 自带函数
get_browser(null);
返回类似

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] =>
)

使用get_browser() 参考手册链接

$browser = get_browser(null, true);
print_r($browser);

人气教程排行