PHP 手冊中有檢查瀏覽器版本的函數 get_browser()
但這個函數沒經過設定是無法使用的
echo $_SERVER['HTTP_USER_AGENT']; echo "<br/>"; // 第一個參數可以指定user agent,第二個參數設為true回傳陣列,否則回傳物件 $browser = get_browser(null, true); print_r($browser);
執行
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Warning: get_browser(): browscap ini directive not set in xxx.php
設定
一、下載 browscap.ini
可選下載PHP browscap.ini的版本有輕量版、完整版或一般版本 ,此檔必須手動更新,以免新版本的瀏覽器無法辨識。
# cd /etc/php.d/
# wget http://browscap.org/stream?q=PHP_BrowsCapINI -O browscap.ini
二、修改 php.ini ,我使用絕對路徑,以免出現錯誤
尋找[browscap]這個tab,加入一行
browscap = /etc/php.d/browscap.ini
重新啟動 APACHE
三、重新執行
我下載完整版本後執行的結果
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0 0
Array ( [browser_name_regex] => ~^mozilla/5\.0 \(.*windows nt 6\.1.*wow64.*\) gecko.* firefox/53\.0.*$~ [browser_name_pattern] => Mozilla/5.0 (*Windows NT 6.1*WOW64*) Gecko* Firefox/53.0* [parent] => Firefox 53.0 [browser_bits] => 32 [platform] => Win7 [platform_version] => 6.1 [platform_description] => Windows 7 [platform_bits] => 64 [platform_maker] => Microsoft Corporation [win64] => 1 [device_name] => Windows Desktop [device_code_name] => Windows Desktop [comment] => Firefox 53.0 [browser] => Firefox [browser_type] => Browser [browser_maker] => Mozilla Foundation [version] => 53.0 [majorver] => 53 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [javascript] => 1 [javaapplets] => 1 [cssversion] => 3 [device_type] => Desktop [device_pointing_method] => mouse [renderingengine_name] => Gecko [renderingengine_version] => 53.0 [renderingengine_description] => For Firefox, Camino, K-Meleon, SeaMonkey, Netscape, and other Gecko-based browsers. [renderingengine_maker] => Mozilla Foundation [browser_modus] => unknown [minorver] => 0 [alpha] => [beta] => [win16] => [win32] => [backgroundsounds] => [vbscript] => [activexcontrols] => [ismobiledevice] => [istablet] => [issyndicationreader] => [crawler] => [isfake] => [isanonymized] => [ismodified] => [aolversion] => 0 [device_maker] => unknown [device_brand_name] => unknown )
相當的詳細
參考資料
[1] Rewi Blog http://rewgaz.wordpress.com/2012/11/20/howto-install-and-use-browscap-respectively-get_browser-for-php-on-ubuntu-debian/
[2] PHP Warning: Cannot open 'extra/browscap.ini' for reading in Unknown on line 0
此錯誤發生原因為 php.ini的設定檔指定的 browscap.ini找不到,請將他設為絕對路徑或註解掉";"可解決。
[3] PHP函式手冊 http://php.net/manual/en/function.get-browser.php
原文編修 2013-08-31 01:04:41