QuickStart - GaretJax/phpbrowscap GitHub Wiki

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

The 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.

Setup using Composer

Use the following instructions if you want to quickly setup a working Browscap environment by taking advantage of the Composer facilities:

  • Put a file named composer.json at the root of your project, containing your project dependencies:
{
    "require": {
        "garetjax/phpbrowscap": "~2.0"
    }
}
  • 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/autoload.php';

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

// Create a new Browscap object (loads or creates the cache)
$bc = new Browscap('path/to/the/cache/dir');

// Get 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

Setup without Composer

Use the following instructions if you simply want to use the Browscap class without exploiting the Composer management features:

  • Download the package and unpack the tarball or zipball of the latest version from the downloads page
  • Create a .php file with the following content:
<?php

// Loads the class
require 'path/to/Browscap.php';

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

// Create a new Browscap object (loads or creates the cache)
$bc = new Browscap('path/to/the/cache/dir');

// Get 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 haven't 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
)

Additional Information

The get_browser() function returns a result with lowercased properties. If you want the same result with the Browscap class you need to set the lowercase property to true.

<?php

// Loads the class
require 'path/to/Browscap.php';

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

// Create a new Browscap object (loads or creates the cache)
$bc = new Browscap('path/to/the/cache/dir');
$bc->lowercase = true;

...
⚠️ **GitHub.com Fallback** ⚠️