views:

376

answers:

10

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?

+3  A: 

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.

RichieHindle
Note that this information is sent by the client, and can be set to anything (One can put "Hellon World!" in it if he wants) ; So you can **try** to detect the user-agent, but your application must always work, not depending on this information !
Pascal MARTIN
+2  A: 

You could look at $_SERVER['HTTP_USER_AGENT']

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

Tom Haigh
+1  A: 

$_SERVER[''HTTP_USER_AGENT'];

detour
+1  A: 

This superglobal variable:

$_SERVER['HTTP_USER_AGENT'];

is what you need ;-)

Time Machine
+3  A: 

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.

PetersenDidIt
note that you need a recent browscap.ini set up etc.
Tom Haigh
+1  A: 

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.

Simone Carletti
A: 

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.

Chacha102
+2  A: 

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);
codemonkey
+1  A: 
$_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.

cpharmston
+2  A: 

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");
}
Alex L