使用 PHP 內建函數 get_browser() 判斷是否是行動瀏覽器

來源:互聯網
上載者:User

標籤:

get_browser — 擷取瀏覽器具有的功能。該函數通過尋找 browscap.ini 檔案中的瀏覽器資訊,嘗試檢測使用者的瀏覽器所具有的功能。

由於許可證的額問題,PHP 未提供瀏覽器功能檔案,可以從 Browscap 得到瀏覽器功能檔案,可以在 http://browscap.org/ 下載 php_browscap.ini 檔案。

下載之後,需要在 php.ini 中設定(Win 下):

browscap=D:\wamp\bin\php\php5.5.12\browscap.ini

或者(Linux 下)

browscap=/usr/local/php/lib/php/browscap.ini

 

此時可以在 php 檔案中列印出 get_browser()

var_dump(get_browser());

輸出:

object(stdClass)#1 (15) {  ["browser_name_regex"]=>  string(101) "~^mozilla/5\.0 \(.*windows nt 10\.0.*\) applewebkit/.* \(khtml, like gecko\) chrome/49\..*safari/.*$~"  ["browser_name_pattern"]=>  string(85) "Mozilla/5.0 (*Windows NT 10.0*) AppleWebKit/* (KHTML, like Gecko) Chrome/49.*Safari/*"  ["parent"]=>  string(11) "Chrome 49.0"  ["platform"]=>  string(5) "Win10"  ["comment"]=>  string(11) "Chrome 49.0"  ["browser"]=>  string(6) "Chrome"  ["browser_maker"]=>  string(10) "Google Inc"  ["version"]=>  string(4) "49.0"  ["majorver"]=>  string(2) "49"  ["device_type"]=>  string(7) "Desktop"  ["device_pointing_method"]=>  string(5) "mouse"  ["minorver"]=>  string(1) "0"  ["ismobiledevice"]=>  string(0) ""  ["istablet"]=>  string(0) ""  ["crawler"]=>  string(0) ""}

 

可以通過以下代碼來判斷是否是移動端瀏覽器:

<?php$ismobiledevice = get_browser()->ismobiledevice;if($ismobiledevice) {    //移動端裝置} else {    //PC端}

 

參考:

<PHP Cookbook>,3rd

 

使用 PHP 內建函數 get_browser() 判斷是否是行動瀏覽器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.