Jump to content

Echo diff stylesheets dependin on browser


BoltZ

Recommended Posts

<?php
if ($_SERVER['HTTP_USER_AGENT'] == "Mozilla Firefox")
{
echo '<link rel="stylesheet" type="text/css" href="stylesheet.css" />';
}
else if ($_SERVER['HTTP_USER_AGENT'] == "Microsoft Internet Explorer")
{
echo '<link rel="stylesheet" type="text/css" href="iestyle.css" />';
}

?>

This code does not print any stylesheet for any browser. What am I doing wrong?

first, the HTTP_USER_AGENT is not simply "Mozilla Firefox" or "Microsoft Internet Explorer".

 

you could set up get_browser(), which will translate the user agent into something you can work with:

http://us3.php.net/function.get-browser

read the Notes section for how to set up browsercap.ini

 

the other option is to use the more common method. this involves coding it to work in firefox, then having an IE conditional comment. more info on that is here:

http://www.quirksmode.org/css/condcom.html

 

BUT, i'm curious as to why you need different CSS files. usually if you think you need separate files, you are doing something wrong. you should be able to do it all with one CSS file.

You convinced me my older friend. It makes sense since this will be a bulletin board system code I will have to make sure it is usable by everyone and not what is easier for me to code. ;)  I shall go research this get_browser function.

 

(man you wouldnt believe the kind of stuff I have learned from trying to create forums, i definately recommend people trying it)

to get get_browser() working, you need to download this file:

http://browsers.garykeith.com/stream.asp?Lite_PHP_BrowsCapINI

save it somewhere (i saved it in the 'extras' folder where PHP is installed). then update this line in your php.ini file:

browscap = /path/to/browscap.ini

I would just do this:

 

<?php

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE)
{
echo '<link rel="stylesheet" type="text/css" href="stylesheet.css" />';
}
else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{
echo '<link rel="stylesheet" type="text/css" href="iestyle.css" />';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.