Browser Detection With PHP Browscap

Posed under PHP on April 9, 2009.

Article

Comments

Post

Dave

Apr 09 2009

Browsecap is an interesting solution for handling generic web browsers and various bots however, if people are looking for something to handle specifically mobile devices, please consider the WURFL project (http://wurfl.sourceforge.net/) instead.

A cursory glance at the mobile entries for Nokia and SonyEricsson user-agents in Browsecap shows numerous issues with the capabilities e.g. frames, cookies and javascript are not supported across the whole range of devices. WURFL has device specific capabilities that are much better suited for mobile apps.

Evan Byrne

Apr 09 2009

Dave, I haven't tried the Browscap method on any mobile devices yet, so I can't see exactly what is being output. Just be aware that print_r() does not display anything for the PHP values 'true' and 'false'. So to find out if frames are supported then you would have to do something like this:

if($data->Frames)
{
  echo "Supported!";
}
else
{
  echo "Not Supported!";
}

Dave

Apr 09 2009

I meant that the actual capabilities that are listed in the Browsecap INI file are not correct. For example: frames are not supported on most mobile devices (which makes sense considering the medium and lack of screen space).

If you want an example of what could happen consider the following handsets: SonyEricssonT610, SonyEricssonW200i, SonyEricssonW880i. They are 3 completely different handsets, with different browser software and the T610 is (effectively) ancient. Using Browsecap, these would all be identified as mobile devices (correct), but they would get the same range of capabilities with the exact same properties. In this case that would be a serious issue as the T610 is extremely limited in its capabilities.

Admittedly that is an extreme example but it is something to bare in mind - and this does not account for the 3 different screen resolution of the mentioned devices either; just to make things more interesting :)

Evan Byrne

Apr 09 2009

Very true, but I doubt very many users need to know the screen size of EVERY known device that can view web pages. Browscap is good for stuff such as site statistics. If you need to know stuff such as browser window size then you should be using javascript anyways.

Tyler Menezes

Apr 15 2009

"Just be aware that print_r() does not display anything for the PHP values 'true' and 'false'."

But var_dump() does.

Chris

Apr 25 2009

Anyone know why I would be getting this error when I run test.php?

Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /nfs/c02/h06/mnt/40270/domains/mydomain.com/html/trcaa/guest/browscap/Browscap.php on line 34

Jonathan Stoppani

Aug 05 2009

Hi,
glad you posted an article about my project... ;-)

only a little correction: the php_browscap.ini file is automatically downloaded and updated.

Siam

Sep 07 2009

Can ne one tell me why i am getting this error message??

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\1.php on line 3

I have installed php_browscap.ini and i am using xammp for server.

I have written the script as..

<?php

localFile = 'php_browscap.ini';

$data = $bc->getBrowser();
echo$data;


?>

Evan Byrne

Sep 07 2009

Siam, that is a strange error indeed. I did notice that in the snippet you posted that you are trying to echo the data. You cannot do that because $bc->getBrowser() returns an object. Use print_r() or var_dump() instead.

Siam

Sep 07 2009

Siam

Again its me.Now i have used var_dump() but i got another error message:

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\1.php on line 5

I have created my test.php file on htdocs of xammp where i have written the following codes:
<?php

localFile = 'php_browscap.ini';

$data = $bc->getBrowser();
var_dump($data);

?>
Now, should i copy the Browser.php(as you mentioned in the article) in htdocs???

Siam

Sep 07 2009

Name of my file is not "test.php" it is "1.php" as you can see on the error message

Evan Byrne

Sep 07 2009

You are missing the $ before "localFile". It should be:

$localFile = 'php_browscap.ini';

Siam

Sep 07 2009

I correct it to $localFile.But now it gives...

Fatal error: Call to a member function getBrowser() on a non-object in C:\xampp\htdocs\1.php on line 7

(interrupting you too much evan!!!)

Evan Byrne

Sep 07 2009

Oh dear, my mistake! Due to an error in my typing some of the code didn't show up in the article. Please take another look at the code now that I've fixed the article.

Sorry about the trouble.

Post Comment

Capcha