Identifying the browser at server side in php

江枫思渺然 提交于 2019-12-11 03:48:48

问题


I have a website. When the user clicks on a particular page (say identify.php) I want to find the type of browser the client is using. The browser might be mozilla, IE, opera, chrome or any other mobiles device such as SonyEricssonK610i, SAMSUNG-SGH-E370, SonyEricssonT700 or NokiaN73-1.

Is this possible to detect the user browser?


回答1:


You need to look at:

$_SERVER['HTTP_USER_AGENT']

That will contain the User-Agent string for the browser. Beware that almost all browsers claim to be "Mozilla" for compatibility reasons - you need to look for specific text for each browser within that header, eg. "MSIE" for Internet Explorer.

Some examples:

My Firefox calls itself Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 4.0.20506)

My IE7 calls itself Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; http://bsalsa.com) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 4.0.20506)

Note all the trickery in the IE one, eg. claiming to be multiple versions.




回答2:


Try the php function get_browser();

http://us3.php.net/function.get-browser

Or you can try out one of the many lighter weight scripts in the comments of that page.




回答3:


You could look at $_SERVER['HTTP_USER_AGENT']

http://php.net/manual/en/reserved.variables.server.php




回答4:


We've started using browsecap and believe it's a more reliable solution that trying to accurately determine browser type by parsing the HTTP_USER_AGENT string ourselves.

edit php.ini file (or .htaccess file) to include:

php_value browscap                        '/path/to/my/browscap/browscap.ini'  

... then you're function call in php is:

$browser = get_browser(null, true);



回答5:


For mobiles, use WURFL

$myDevice = new wurfl_class($wurfl, $wurfl_agents);
$myDevice->GetDeviceCapabilitiesFromAgent($_SERVER["HTTP_USER_AGENT"]);
if ( $myDevice->is_wireless_device ) {
  header("Content-Type: text/vnd.wap.wml");
}



回答6:


$_SERVER[''HTTP_USER_AGENT'];




回答7:


This superglobal variable:

$_SERVER['HTTP_USER_AGENT'];

is what you need ;-)




回答8:


PHP provides a function called get_browser that checks the user-agent header and tries to guess the browser against a whitelist. It's quite limited, but it works for the most common browsers.

If it doesn't fit your application, then you might need to create a custom function.




回答9:


$_SERVER['HTTP_USER_AGENT']

This variable provides you with the User Agent string of the requesting device. From here, you can use regex to cheThe number of reasons that you shouldn't do this are plentiful:

  1. User Agents are easily spoofed and thus unreliable
  2. User Agent strings are inconsistent, constantly changing (i.e. not future-proof) and not easily interpreted (though here is a list of them)

I suggest doing capability checking with Javascript, as jQuery does.




回答10:


Use the HTTP_USER_AGENT string to detect the browser, like everyone else has mentioned. Here is an example of what it will look like:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.1 Safari/532.0.

Just record the values for various browsers, and then check for keywords like Chrome. Most likely you will want to check for Webkit or other rendering agents because that will affect how the page is displayed.



来源:https://stackoverflow.com/questions/1180340/identifying-the-browser-at-server-side-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!