user93796
user93796

Reputation: 18379

Identifying the browser at server side in php

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?

Upvotes: 2

Views: 1813

Answers (10)

Alex L
Alex L

Reputation: 8449

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

Upvotes: 2

c_harm
c_harm

Reputation:

$_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.

Upvotes: 1

codemonkey
codemonkey

Reputation: 2665

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);

Upvotes: 2

Tyler Carter
Tyler Carter

Reputation: 61557

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.

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176362

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.

Upvotes: 1

PetersenDidIt
PetersenDidIt

Reputation: 25620

Try the php function get_browser();

https://www.php.net/function.get-browser

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

Upvotes: 3

user142019
user142019

Reputation:

This superglobal variable:

$_SERVER['HTTP_USER_AGENT'];

is what you need ;-)

Upvotes: 1

detour
detour

Reputation: 123

$_SERVER[''HTTP_USER_AGENT'];

Upvotes: 1

Tom Haigh
Tom Haigh

Reputation: 57815

You could look at $_SERVER['HTTP_USER_AGENT']

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

Upvotes: 2

RichieHindle
RichieHindle

Reputation: 281395

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.

Upvotes: 5

Related Questions