Browser Dependent Cascading Style Sheets (CSS) in PHP

By: DN
Posted: December 16, 2004

Everyday webmasters are learning more about making tables-less web designs. Some problems they face are incompatibilities between browsers. If you can make a CSS layout look consistent in Internet Explorer and Mozilla Firefox, then you have done well. Believe it or not, people are still using the “stone age” version 4 of Netscape Navigator which has horrible CSS support.

Your work around for this problem is to use browser-dependent style sheets. This can easily be done in PHP.

PHP has a special built-in function, get_broswer(), to obtain certain information about a user’s browser. We will be using it in our following code.

$browser = get_browser();

// *Use a different CSS file if the user's browser is Netscape version 4*

$css_file = (

$browser->browser == 'Netscape' && $browser->version == 4

) ? 'ns4-style.css' : 'style.css';

This conditional statement can be tweaked to allow you to control browser-based content.

As of PHP 4.3.4, it’s still necessary to specify the location of the browscap.ini file in your php.ini file. You can also specify the location in your httpd.conf file on Apache using: php_admin_value browscap /path/to/browscap.ini

Note: As of 2017, many more advanced ways exists for browser-dependent page content (JavaScript, CSS, etc).