Recipe 8.5. Detecting Different Browsers


8.5.1. Problem

You want to generate content based on the capabilities of a user's browser.

8.5.2. Solution

Use the object returned by get_browser( ) to determine a browser's capabilities, as shown in Example 8-17.

Getting browser information

<?php $browser = get_browser(); if ($browser->frames) {     // print out a frame-based layout } elseif ($browser->tables) {     // print out a table-based layout } else {     // print out a boring layout } ?>

8.5.3. Discussion

The get_browser( ) function examines the environment variable (set by the web server) and compares it to browsers listed in an external browser capability file. Due to licensing issues, PHP isn't distributed with a browser capability file. The "Obtaining PHP" section of the PHP FAQ (http://www.php.net/faq.obtaining) lists http://www.garykeith.com/browsers/downloads.asp as a source for a browser capability file. Download the php_browscap.ini file from that site.

Once you download a browser capability file, you need to tell PHP where to find it by setting the browscap configuration directive to the pathname of the file. If you use PHP as a CGI, set the directive in the php.ini file, as in Example 8-13.

Setting browscap in php.ini

browscap=/usr/local/lib/php_browscap.ini

Many of the capabilities get_browser( ) finds are shown in Table 8-1. For user-configurable capabilities such as javascript or cookies, though, get_browser( ) just tells you if the browser can support those functions. It doesn't tell you if the user has disabled the functions. If JavaScript is turned off in a JavaScript-capable browser or a user refuses to accept cookies when the browser prompts him, get_browser( ) still indicates that the browser supports those functions.

Table 8-1. Browser capability object properties

Property

Description

platform

Operating system the browser is running on (e.g., Windows, Macintosh, Unix, Win32, Linux, MacPPC)

version

Full browser version (e.g., 5.0, 3.5, 6.0b2)

majorver

Major browser version (e.g., 5, 3, 6)

minorver

Minor browser version (e.g., 0, 5, 02)

frames

1 if the browser supports frames

tables

1 if the browser supports tables

cookies

1 if the browser supports cookies

backgroundsounds

1 if the browser supports background sounds with <embed> or <bgsound>

vbscript

1 if the browser supports VBScript

javascript

1 if the browser supports JavaScript

javaapplets

1 if the browser can run Java applets

activexcontrols

1 if the browser can run ActiveX controls


8.5.4. See Also

Documentation on get_browser( ) at http://www.php.net/get-browser.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net