My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
QuickStart  
How to quickly setup your browser capabilities script without get_browser().
Phase-Deploy, Featured
Updated Feb 4, 2010 by st.jonat...@gmail.com

Code

How to quickly setup your browser capabilities script without get_browser():

  1. Download the package and unpack it
  2. Upload everything on your server (You haven't to modify the Browscap class!)
  3. Change permissions on Browscap/cache to 666 (you might need 777)
  4. Create a .php file with the following content:
  5. <?php
    
    // Loads the class
    require 'path/to/Browscap.php';
    
    // Creates a new Browscap object (loads or creates the cache)
    $bc = new Browscap('path/to/the/cache/dir');
    
    // Gets information about the current browser's user agent
    $current_browser = $bc->getBrowser();
    
    // Output the result
    echo '<pre>'; // some formatting issues ;)
    print_r($current_browser);
    echo '</pre>';

Result

Now open your web browser and navigate to the file you just created, you should see a result similar to the following but with your browser's information:

stdClass Object
(
    [browser_name] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18
    [browser_name_regex] => ^mozilla/5\.0 \(macintosh; .; .*mac os x.*\) applewebkit/.* \(.*\) version/3\.1.* safari/.*$
    [browser_name_pattern] => Mozilla/5.0 (Macintosh; ?; *Mac OS X*) AppleWebKit/* (*) Version/3.1* Safari/*
    [Parent] => Safari 3.1
    [Platform] => MacOSX
    [Browser] => Safari
    [Version] => 3.1
    [MajorVer] => 3
    [MinorVer] => 1
    [Frames] => 1
    [IFrames] => 1
    [Tables] => 1
    [Cookies] => 1
    [BackgroundSounds] => 1
    [JavaApplets] => 1
    [JavaScript] => 1
    [CSS] => 2
    [CssVersion] => 2
    [supportsCSS] => 1
    [Alpha] => 
    [Beta] => 
    [Win16] => 
    [Win32] => 
    [Win64] => 
    [AuthenticodeUpdate] => 
    [CDF] => 
    [VBScript] => 
    [ActiveXControls] => 
    [Stripper] => 
    [isBanned] => 
    [WAP] => 
    [isMobileDevice] => 
    [isSyndicationReader] => 
    [Crawler] => 
    [AOL] => 
    [aolVersion] => 0
    [netCLR] => 
    [ClrVersion] => 0
)
Comment by vbh...@gmail.com, May 23, 2008

Hi Jonathan, I just incorporated your superb Browscap.php class file into the next version of Republic Locomotive Works web site. On the first load of the cache I got the following errors:

The following was repeated 10 times: 

[23-May-2008 11:03:40] PHP Notice: Undefined index: Seamonkey 1.1 in /Components/class.Browscap.php on line 342

The following was repeated 19 times: 

[23-May-2008 11:03:40] PHP Notice: Undefined index: Motorola Internet Browser in /Components/class.Browscap.php on line 342

After the first (cache load?) browser display, the error's disappeared.

Line 342 is the following assignment:

$browsers[$user_agent]['Parent'] = $user_agents_keys[$parent];

It appears that the correct assignment should be:

`if (array_key_exists($parent,$user_agents_key)

$browsers[$user_agent]['Parent'] = $user_agents_keys[$parent];`

The existence of the index in $user_agents_keys could also be checked on line 340 as well.

Other than this minor (database protection) issue, the installation was as smooth as possible. Thanks for sharing your class. --- /Bruce Hunt Webmaster, Republic Locomotive Works

Comment by project member st.jonat...@gmail.com, May 24, 2008

Hi Bruce, thanks for your comment, I admit that I'm not checking if the parent exist, but it should always. The error is in the name of the parent browser which doesn't is the same as the parent property of the current browser, I've already mailed Gary Keith asking for the correction of the database, probably in the next days it'll be corrected.

Regards, Jonathan

Comment by alexa...@gmail.com, May 24, 2008

Hi Jonathan, just wondering if there is any way to use this class without write permissions on the web server? Thanks, Alex

Comment by project member st.jonat...@gmail.com, May 26, 2008

@Alex: I see it hard with the current implementation, one possible solution is to disable the automatic updating (it's possible using the $doAutoUpdate property to false) and then do the updating the file with another server process (or by hand), the second possibility is to modifiy the class to use a sql database, but it's pretty hard to implement.

Comment by alexa...@gmail.com, May 26, 2008

I see. Thanks for the response!

Comment by ktama...@gmail.com, Jul 7, 2008

Hey,

thanks for the great class - it works well! However, this user agent string - despite the browscap.ini file contains it - recognized as Unknown: "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080313 SeaMonkey?/1.1.9" - I'm not sure where is the problem exactly, I'll start to reverse-engineer it...

regards, Tamas

Comment by ktama...@gmail.com, Jul 7, 2008

Hey,

thanks for the great class - it works well! However, this user agent string - despite the browscap.ini file contains it - recognized as Unknown:

"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080313 SeaMonkey?/1.1.9"
- I'm not sure where is the problem exactly, I'll start to reverse-engineer it...

regards, Tamas

Comment by ericbrug...@gmail.com, Jul 28, 2008

get_browser from php itselve is 50% faster then your get_browser function... to bad, hoped that this would speedup my scripting but it didn't!

Hopefully next version will be faster ;)

Comment by project member st.jonat...@gmail.com, Aug 9, 2008

@Tamas: The SeaMonkey? problem is due to an error in the browscap.ini file maintained by Gary Keith. I alread sent him various mails but the error is always there... i can do much of myself, maybe you can try to contact him.

@ericbruggema: my class was faster wen i developed it (something around php 4.4) and i think it is always faster when you have to parse many user agents (the first one it has to load the whole database). After 3-4 queries it should be faster.

Cheers, Jonathan

Comment by ericbrug...@gmail.com, Aug 12, 2008

@st.jonathan: normaly users parse with php one useragent at a time... so its much slower in my case :(

Comment by project member st.jonat...@gmail.com, Aug 13, 2008

@ericbruggema: You're right. I never tried, but i think a RDBMS backend could solve this problem. I'll look forward to integrate at least memcache support in the next version. This should help loading the cache and speed up the whole thing.

Comment by simon.sp...@gmail.com, Aug 14, 2008

How about using SQLite ?

Comment by DTiger.T...@gmail.com, Aug 25, 2008

Hi, 1> A little trouble to access updates... I found the 'version-date' page here: http://browsers.garykeith.com/versions/version-date.asp 2> 1.1? is still a trouble, most of dependant browsers are written 'Seamonkey' not 'SeaMonkey?'. I just informed Gary. 3> Working in Thailand, I do have some thai agents... I am thinking about written an add-on on browscap.ini, and look to modify browscap.php to include it. Any trouble to do that? Did anyone of you ever did that? Thanks Ben

Comment by project member st.jonat...@gmail.com, Aug 26, 2008

1) The new version of the class is uptaded with the new url 2) Gary is responsible for that, i'll look to include a vase insensitive fallback in case of a negative lookup in the next version, but this is still not my fault. 3) no idea... ;-)

Greets

Comment by project member st.jonat...@gmail.com, Sep 17, 2008

Hi to all, I've created a database based version of the class and it's working.. but... there is not really speed improvement (File: 0.0508 and DB: 0.0593 to retrieve 100 times the same useragent) on many queries.

But by the retrieving of a single useragent, there is some difference: - File: ~30ms (yes, it has to load the cache) - DB: ~2ms so, it is a 1500% speed improvement... ;-) I'll look forward to refactor the whole thing and upload a pretty version of the class.

One more thing: after the first cache load of the file-based version, each useragent is processed in something about 0.2ms. If a memcache version could speed up the cache loading thing there could be another great speed improvement.

Bye bye

Comment by ericbrug...@gmail.com, Oct 7, 2008

Hi Jonathan,

Hope to see the version with mysql soon, can use it for a large project! if possible contact me when you release it!

Comment by mx3des...@gmail.com, Oct 13, 2008

Hi, Could you include further 'fool proof' documentation for cache settings if possible please? I'm finding it difficult to get the cache system to update itself. Thanks, Jay

Comment by RQuadling@gmail.com, Oct 16, 2008

Hi.

Just come across this code. Looks good and will be useful to me.

A quick question regarding the class and the updateCache() function. Considering this is called during getBrowser() would it not be worth making this a private method also?

And if the answer is yes, then we are now down to a single method besides construct(), so merging them into a single static method which calls the constructor when needed (essentially a singleton as I can't see why you'd need multiple instances of Browscap).

So ending up with ...

require_once 'path/to/Browscap.php'; $current_browser = Browscap::getBrowser('path/to/the/cache/dir', 'user_defined_agent_string', 'option_to_return_as_array_or_object');

Sure, not essential, but does make sure only 1 instance of the class is loaded.

Other than that, excellent work.

Regards,

Richard Quadling.

Comment by ericbrug...@gmail.com, Oct 21, 2008

Is there also a way to detect bots or other crawlers?

Comment by ericbrug...@gmail.com, Oct 21, 2008

Another error found:

Error type: NOTICE / 8 String: Undefined index: Pogodak File: /home/gfxstatcom/domains/gfxstat.com/public_html/beta/function_getbrowser.php Line: 342

Comment by project member st.jonat...@gmail.com, Oct 22, 2008

@mx3design: To let the cache update itself you have to do exactly nothing... maybe the only thing you have to do is to change the cache directory permission to something higher to allow php to write inside.

@RQuading: for a basic usage you are completely right, but for some advanced logic which includes external cache updates triggers or which is based on different versions of browscap this is not possible

Comment by deruit...@gmail.com, Feb 23, 2009

Thanks for this class. Works like a charm :)

Comment by l...@lazd.net, Aug 19, 2009

I tried out this class in hopes of speeding up PHP's get_browser(). The machine was a dual core Xeon E3110 3.16GHz, 4GB RAM, Apache/2.2.11 (Unix) and PHP 5.2.9 - Load Avg: 0.20, 0.20, 0.13. Here are the results of my tests:


Using PHP's get_browser() and php_browscap.ini: 0.0370211601257 sec

Using Browscap.php class and php_browscap.ini: 0.0388770103455 sec

Using PHP's get_browser() and lite_php_browscap.ini: 0.0258259773254 sec

Using Browscap.php class and lite_php_browscap.ini: 0.0213708877563 sec

Comment by jpbarb...@gmail.com, Aug 27, 2009

Hi Jonathan,

first, thanks for your class, she is great, BUT i've got a problem with her...

When I launch the page in firefox for example, browscap.ini is correctly download and the cache.php file are created, but the class send back me an empty array... I tried to lauch in my local machine and online, but it's the same result !

Thanks for your help !

Comment by sebastia...@gmail.com, Sep 2, 2009

@jpbarbaud: I think you may have the same problem as I -- PHP 5.3. See my bug report and the followups at http://code.google.com/p/phpbrowscap/issues/detail?id=11

Comment by jpbarb...@gmail.com, Sep 2, 2009

@sebastian.schleussner : Thanks for your help, problem fix ;-). I will recommended your class to everyone around me ;-)

Comment by sebastia...@gmail.com, Sep 3, 2009

Glad I could help you! Just want to make sure it's understood this is still Jonathans's class, I'm a mere hacking user. :-)

Comment by efluit...@gmail.com, Sep 15, 2009

thx for the great code man, really helped me out!!

Comment by cem...@gmail.com, Sep 30, 2009

My cache.php has empty arrays. I noticed this warning: Warning: syntax error, unexpected $end, expecting ']' in /path/to/browscap.ini on line 49

in /path/to/Browscap.php on line 308

Comment by cem...@gmail.com, Sep 30, 2009

It is the php 5.3.0 problem. Solution in http://code.google.com/p/phpbrowscap/issues/detail?id=11

Comment by dei...@gmail.com, Dec 21, 2009

hello

in my download is only the browscap folder, i think than need the others folders calls in the examples for example the 'cache' folder. i havn't.... help me... sorry for my bad english, i not american :P

Comment by upstagel...@gmail.com, Jan 26, 2010

Hey [GaretJax|St.Jonathan],

This is a great piece of work. I'm hoping I can replace my little cobbled together hack with it. First, though, two things:

1. This routine does not return the OS version for Mac useragents. I scanned through the code and didn't see where to amend that.

2. My browser is Camino

useragent: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en; rv:1.9.0.15) Gecko/2009102617 Camino/2.0 (like Firefox/3.0.15)"
and the regex picks out the "like Firefox" string and incorrectly returns Firefox as the browser name.

Any chance of addressing these?

Thanks!

Comment by upstagel...@gmail.com, Jan 26, 2010

Ok, I fixed the MacOS version part:

In the Browscap.php file, I added the three lines following, immediately before the return statement in the public function getBrowser() declaration/definition:

     if ( strpos ( $user_agent, "Intel Mac OS X" ) ) $platform = substr ( strstr( $user_agent, "Intel" ), 0 , strpos( strstr( $user_agent, "Intel" ), ";" ) );
     if ( strpos ( $user_agent, "PPC Mac OS X" ) ) $platform = substr ( strstr( $user_agent, "PPC" ), 0 , strpos( strstr( $user_agent, "PPC" ), ";" ) );
     if ( isset ( $platform ) ) $array['MacOSVersion'] = $platform; 
Comment by markho...@gmail.com, Mar 20, 2010

Is there any way we can store the browscap.ini in a MySQL table rather than a local file, and then have this script refer to the table?

Comment by FranckPr...@gmail.com, Apr 20, 2010

I have isMobileDevice == 0 for this mobile devices :

NOKIA N900 (Firefox mobile: Maemo)

"Mozilla/5.0 (X11; U; Linux armv7l; fi-FI; rv:1.9.2a1pre) Gecko/20090928 Firefox/3.5 Maemo Browser 1.4.1.19 RX-51 N900" http://maemo.nokia.com/features/maemo-browser/ http://deviceatlas.com/devices/Nokia/N900/entry/1666893

HTC TOUCH (windows mobile 6)

"PPC; 240x320; HTC_P3450/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 6.12)" http://deviceatlas.com/devices/HTC/P3450/entry/1059139

Comment by gab...@yahoo.fr, Apr 20, 2010

browscap didn't return the version of the browser for Blackberry mobile. UA Example : "BlackBerry9700?/5.0.0.351 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/123"


Sign in to add a comment
Powered by Google Project Hosting