• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

PHP – How to Get Browser Properties and Capabilities – get_browser()

Posted in Enterprise Web, PHP/MySQL, Programming Techniques. on Thursday, July 1st, 2010 by Aneeska Tags: browscap, browscap.ini, browser capability, browser properties, gary keith, get_browser, http_user_agent, ini, ismobiledevice, is_mobile_device, mobile, mobile application, php, php.ini, web
Jul 01

In this tutorial, learn about the get_browser() PHP function and its configuration. We’ll learn how to use Gary Keith’s browser information database (browscap.ini) with the PHP browscap directive.

I wanted one of my web applications to have a different appearance on mobile phones and devices. I didn’t really want to set up an entirely different domain or use a sub domain particularly for mobile devices (like http://mobile.facebook.com). Instead I want to tackle the device inside my application logic so that all users access the same URL.

PHP’s $_SERVER['HTTP_USER_AGENT'] is cool. You can play around this variable and achieve what you want. But you don’t get all you want. Let’s look at the string output of the above variable on my browser:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)

This gives you some information. I wanted an easy way to see whether the request was from a mobile device.

PHP has this function, get_browser which attempts to determine the capabilities of the user’s browser, by looking up the browser’s information in the browscap.ini file.

In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system. browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here. While browscap.ini contains information on many browsers, it relies on user updates to keep the database current.

First, Download the browscap.ini file for PHP from the above website. There are many files and this is the one you need: Gary Keith’s browscap.ini for PHP

Now, open your PHP.ini file and find for “browscap”. You will see something like this:

[browscap]
;browscap = extras\browscap.ini

Remove the semicolon and change the path to point to the php_browscap.ini you just downloaded. Your PHP.ini directive now looks like this:

[browscap]
browscap = C:\Inetpub\PHP5\extras\php_browscap.ini

Save the PHP.ini file and restart your Web server.

Now use the get_browser function in your PHP code to get user’s browser properties and capabilities.

<?php
$browser = get_browser(null, true);
echo "<pre>";
print_r($browser);
echo "</pre>";
?>

This is the output I got when I accessed the script using a Computer Browser.

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .*; windows nt 5\.1; .*; rv:1\.9\.2.*) gecko/.* firefox/3\.6.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; *; Windows NT 5.1; *; rv:1.9.2*) Gecko/* Firefox/3.6*
    [parent] => Firefox 3.6
    [platform] => WinXP
    [win32] => 1
    [browser] => Firefox
    [version] => 3.6
    [majorver] => 3
    [minorver] => 6
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [javaapplets] => 1
    [javascript] => 1
    [cssversion] => 3
    [supportscss] => 1
    [alpha] =>
    [beta] =>
    [win16] =>
    [win64] =>
    [backgroundsounds] =>
    [cdf] =>
    [vbscript] =>
    [activexcontrols] =>
    [isbanned] =>
    [ismobiledevice] =>
    [issyndicationreader] =>
    [crawler] =>
    [aol] =>
    [aolversion] => 0
)

When I accessed the same script using a mobile phone, here’s what I got:

Array
(
    [browser_name_regex] => ^.*nokia.*/.*$
    [browser_name_pattern] => *Nokia*/*
    [parent] => Nokia
    [browser] => Nokia
    [tables] => 1
    [cookies] => 1
    [ismobiledevice] => 1
    [version] => 0
    [majorver] => 0
    [minorver] => 0
    [platform] => unknown
    [frames] =>
    [iframes] =>
    [javaapplets] =>
    [javascript] =>
    [cssversion] => 0
    [supportscss] =>
    [alpha] =>
    [beta] =>
    [win16] =>
    [win64] =>
    [backgroundsounds] =>
    [cdf] =>
    [vbscript] =>
    [activexcontrols] =>
    [isbanned] =>
    [ismobiledevice] =>
    [issyndicationreader] =>
    [crawler] =>
    [aol] =>
    [aolversion] => 0
)

I was just looking at this,

[ismobiledevice] => 1

Interesting uh!

If your shared hosting service doesn’t allow you to modify the PHP.INI file, you would want to use a stand alone implementation of the same concept by Jonathan. http://code.google.com/p/phpbrowscap/


ASK a Question!

Bookmark and Share

1 Comment

  1. Théoden’s Coding Tips » Blog Archive » Problems with PHP’s get_browser() on openSUSE 11.2 on July 27th, 2010

    [...] just spent way too much time trying to debug PHP’s get_browser() on an openSUSE 11.2 server. All I wanted to do was display a funny little note about what web [...]



Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top