Jump to content

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" />';
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.