sb3108sa / phpbrowscap

Automatically exported from code.google.com/p/phpbrowscap
GNU Lesser General Public License v2.1
0 stars 0 forks source link

How to make the call shorter #14

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Here's how I call Browscap:

require('scripts/Browscap.php');
print_r(broswercheck());

There's a tiny bit of tweaking involved in the class file to get the call 
this simple:
1. Add a relative path from Browscap.php to your own cache directory on 
line 153: public $cacheDir= "cache/php";

2. In the "construct" function on line 178, Remove the $cache_path argument 
from between the brackets and add this line beneath the timezone setting:
$cache_dir=dirname(__FILE__).'/'.$this->cacheDir;

3. Add the following function to the very bottom of Browscap.php:
//wrapper function for easier use:
function broswercheck($returnKey=0){
    // Creates a new Browscap object (loads or creates the cache)
    $bc = new Browscap();

    // Gets information about the current browser's user agent
    $b = $bc->getBrowser();
    return($returnKey ? $b[$returnKey] : $b);
}

This makes browser sniffing almost as quick as calling the native php 
version.
Hope this helps!
Cron
http://blog.crondesign.com

Original issue reported on code.google.com by cronoklee@gmail.com on 1 May 2010 at 7:01

GoogleCodeExporter commented 8 years ago
There's a little error there actually - the last line in step 3 should be:
$b->$returnKey rather than $b[$returnKey]

I've actually packed this up and blogged it now so you can download the 
modified file 
here:
http://blog.crondesign.com/2010/05/php-quick-handy-browser-snifferchecker.html

Original comment by cronoklee@gmail.com on 2 May 2010 at 4:55