Skip to content
DominikTo edited this page Feb 8, 2012 · 13 revisions

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

Composer Package Manager

phpbrowscap is available via the Composer package manager. Composer helps you manage your project or libraries' dependencies. You can find the Composer source on GitHub.

Code

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

  • Put a file named composer.json at the root of your project, containing your project dependencies:
{
    "require": {
        "GaretJax/phpbrowscap": "*"
    }
}
  • Install Composer in your project via wget http://getcomposer.org/composer.phar
  • Execute this in the root of your project php composer.phar install
  • Create a .php file with the following content:
<?php

// phpbrowscap follows the PSR-0 standard, so just require the composer autoload
require 'vendor/.composer/autoload.php';

// the Browscap class is in the phpbrowscap namespace, so import it
use phpbrowscap\Browscap;

// 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>';
  • Upload everything to your web server (you don't have to modify the Browscap class!)
  • Change permissions on cache directory (path/to/the/cache/dir, in the example) to be readable/writeable by the user running the PHP process

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
)
Clone this wiki locally