Code
How to quickly setup your browser capabilities script without get_browser():
- Download the package and unpack it
- Upload everything on your server (You haven't to modify the Browscap class!)
- Change permissions on Browscap/cache to 666 (you might need 777)
- Create a .php file with the following content:
<?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
)
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:
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)
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
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
Hi Jonathan, just wondering if there is any way to use this class without write permissions on the web server? Thanks, Alex
@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.
I see. Thanks for the response!
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
Hey,
thanks for the great class - it works well! However, this user agent string - despite the browscap.ini file contains it - recognized as Unknown:
regards, Tamas
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 ;)
@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
@st.jonathan: normaly users parse with php one useragent at a time... so its much slower in my case :(
@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.
How about using SQLite ?
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
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
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
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!
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
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.
Is there also a way to detect bots or other crawlers?
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
@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
Thanks for this class. Works like a charm :)
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
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 !
@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
@sebastian.schleussner : Thanks for your help, problem fix ;-). I will recommended your class to everyone around me ;-)
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. :-)
thx for the great code man, really helped me out!!
My cache.php has empty arrays. I noticed this warning: Warning: syntax error, unexpected $end, expecting ']' in /path/to/browscap.ini on line 49
It is the php 5.3.0 problem. Solution in http://code.google.com/p/phpbrowscap/issues/detail?id=11
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
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
and the regex picks out the "like Firefox" string and incorrectly returns Firefox as the browser name.Any chance of addressing these?
Thanks!
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;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?
I have isMobileDevice == 0 for this mobile devices :
NOKIA N900 (Firefox mobile: Maemo)
HTC TOUCH (windows mobile 6)
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"