using different stylesheets for different browsers (how to)?

using different stylesheets for different browsers (how to)?

am 09.04.2008 18:12:36 von Ronald Raygun

I have spent the last few days trying to create a tableless web page,
using CSS only for layout. I have finally given up. My current css file
works fine for IE - but looks terrible in Netsacpe and Firefox. if I fix
the issue so that it works for Netscape/Firefox, then the page looks
like crap in IE.

I have finally decided to use two different style sheets (one is a minor
modification of teh other),l and generate the HTML page dynamically =-
and use a link to the appropriate stylesheet - depending on the UA
(browser) making teh request for the page.

The (simplified) logic is something like this :

$user_agent = get_user_agent();
if ($user_agent == IEXPLORER)
generate_Iexplorer_page();
else
generate_mozilla_page();


I am new to PHP (I'm a C++ programmer), so I would appreciate it if
someone could show me how to do the following:

1). Write a function to determine the UA (user agent - i.e. browser) type
2). generate html page with the appropriate css file link.

For the purposes for the code snippet, you can assume that the (path)
names of the css files are :

.../mozilla.css (css for mozilla browsers)
.../explorer.css (css for Iexplorer browsers)

Also please assume that the HTML page to be genrated consists of teh ff
simple HTML:



This is a test


Some text goes here ...

Re: using different stylesheets for different browsers (how to)?

am 09.04.2008 18:50:43 von Jerry Stuckle

Ronald Raygun wrote:
> I have spent the last few days trying to create a tableless web page,
> using CSS only for layout. I have finally given up. My current css file
> works fine for IE - but looks terrible in Netsacpe and Firefox. if I fix
> the issue so that it works for Netscape/Firefox, then the page looks
> like crap in IE.
>
> I have finally decided to use two different style sheets (one is a minor
> modification of teh other),l and generate the HTML page dynamically =-
> and use a link to the appropriate stylesheet - depending on the UA
> (browser) making teh request for the page.
>
> The (simplified) logic is something like this :
>
> $user_agent = get_user_agent();
> if ($user_agent == IEXPLORER)
> generate_Iexplorer_page();
> else
> generate_mozilla_page();
>
>
> I am new to PHP (I'm a C++ programmer), so I would appreciate it if
> someone could show me how to do the following:
>
> 1). Write a function to determine the UA (user agent - i.e. browser) type
> 2). generate html page with the appropriate css file link.
>
> For the purposes for the code snippet, you can assume that the (path)
> names of the css files are :
>
> ../mozilla.css (css for mozilla browsers)
> ../explorer.css (css for Iexplorer browsers)
>
> Also please assume that the HTML page to be genrated consists of teh ff
> simple HTML:
>
>
>
> This is a test
>
>
> Some text goes here ...
>
>

Don't. Just use the conditionals available in css. Try a css newsgroup
for more info.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: using different stylesheets for different browsers (how to)?

am 09.04.2008 19:42:23 von nn

On Wed, 09 Apr 2008 17:12:36 +0100, Ronald Raygun
wrote:

>I have spent the last few days trying to create a tableless web page,
>using CSS only for layout. I have finally given up. My current css file
>works fine for IE - but looks terrible in Netsacpe and Firefox. if I fix
>the issue so that it works for Netscape/Firefox, then the page looks
>like crap in IE.
>
>I have finally decided to use two different style sheets (one is a minor
>modification of teh other),l and generate the HTML page dynamically =-
>and use a link to the appropriate stylesheet - depending on the UA
>(browser) making teh request for the page.
>
>The (simplified) logic is something like this :
>
>$user_agent = get_user_agent();
>if ($user_agent == IEXPLORER)
> generate_Iexplorer_page();
>else
> generate_mozilla_page();
>
>
>I am new to PHP (I'm a C++ programmer), so I would appreciate it if
>someone could show me how to do the following:
>
>1). Write a function to determine the UA (user agent - i.e. browser) type
>2). generate html page with the appropriate css file link.
>
>For the purposes for the code snippet, you can assume that the (path)
>names of the css files are :
>
>../mozilla.css (css for mozilla browsers)
>../explorer.css (css for Iexplorer browsers)
>
>Also please assume that the HTML page to be genrated consists of teh ff
>simple HTML:
>
>
>
> This is a test
>
>
> Some text goes here ...
>

I ran into a similar problem and i used the function below. it's far
too simple, but it worked for me.( if you want to return the browser
version return an array that includes both the browser agent and the
browser version. )

in your case you can use instead of $browser_agent='IE'; etc.
$browser_agent='IE.css'; $browser_agent='safari.css'; etc. and then
include this in your header:




function getBrowserAgent(){

if (ereg( 'MSIE
([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version )) {
$browser_version=$log_version[1];
$browser_agent='IE';
} elseif (ereg(
'Safari/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log _version))
{
$browser_version=$log_version[1];
$browser_agent='Safari';
} elseif (ereg(
'Opera/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_ version))
{
$browser_version=$log_version[1];
$browser_agent='Opera';
} elseif (ereg(
'Mozilla/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$lo g_version))
{
$browser_version=$log_version[1];
$browser_agent='Mozilla';
} else {
$browser_version=0;
$browser_agent='Other';
}

return $browser_agent;
}

echo getBrowserAgent();
?>

good luck,

NN

Re: using different stylesheets for different browsers (how to)?

am 09.04.2008 21:19:27 von No_One

On 2008-04-09, NN wrote:
>
> function getBrowserAgent(){
>
> if (ereg( 'MSIE
> ([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version )) {
> $browser_version=$log_version[1];
> $browser_agent='IE';
> } elseif (ereg(
> 'Safari/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log _version))
> {
> $browser_version=$log_version[1];
> $browser_agent='Safari';
> } elseif (ereg(
> 'Opera/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_ version))
> {
> $browser_version=$log_version[1];
> $browser_agent='Opera';
> } elseif (ereg(
> 'Mozilla/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$lo g_version))
> {
> $browser_version=$log_version[1];
> $browser_agent='Mozilla';
> } else {
> $browser_version=0;
> $browser_agent='Other';
> }
>
> return $browser_agent;
> }
>
> echo getBrowserAgent();
> ?>
>
> good luck,
>
> NN


The only problem is not all browser reports are accurate and not all
browsers of the same family support the same css - or anything else for that
matter - the same way.

ken

Re: using different stylesheets for different browsers (how to)?

am 09.04.2008 21:36:25 von nn

On Wed, 09 Apr 2008 14:19:27 -0500, No_One
wrote:

>On 2008-04-09, NN wrote:
>>
>> function getBrowserAgent(){
>>
>> if (ereg( 'MSIE
>> ([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version )) {
>> $browser_version=$log_version[1];
>> $browser_agent='IE';
>> } elseif (ereg(
>> 'Safari/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log _version))
>> {
>> $browser_version=$log_version[1];
>> $browser_agent='Safari';
>> } elseif (ereg(
>> 'Opera/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_ version))
>> {
>> $browser_version=$log_version[1];
>> $browser_agent='Opera';
>> } elseif (ereg(
>> 'Mozilla/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$lo g_version))
>> {
>> $browser_version=$log_version[1];
>> $browser_agent='Mozilla';
>> } else {
>> $browser_version=0;
>> $browser_agent='Other';
>> }
>>
>> return $browser_agent;
>> }
>>
>> echo getBrowserAgent();
>> ?>
>>
>> good luck,
>>
>> NN
>
>
>The only problem is not all browser reports are accurate and not all
>browsers of the same family support the same css - or anything else for that
>matter - the same way.
>
>ken
>
you are right. the traditional method of using javascript is not
accurate either because there are many people who turn off JS
alltogether.

on the function above you could also use the browser_version and also
add a function to determine the platform, but again it's not always
perfect. i've had problems myself with Opera, even though it fully
supports CSS, etc; and of course i've had major headackes with IE6 &
IE7

here is what i use to determine the platform:

if (strstr($_SERVER['HTTP_USER_AGENT'],'Win')) {
$browser_platform='Windows';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Mac')) {
$browser_platform='Mac';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Linux')) {
$browser_platform='Linux';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Unix')) {
$browser_platform='Unix';
} else {
$browser_platform='Other';
}

NN

Re: using different stylesheets for different browsers (how to)?

am 09.04.2008 23:20:16 von No_One

On 2008-04-09, NN wrote:
> On Wed, 09 Apr 2008 14:19:27 -0500, No_One
> wrote:
>
>>On 2008-04-09, NN wrote:
>>>
>>> function getBrowserAgent(){
>>>
>>> if (ereg( 'MSIE
>>> ([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version )) {
>>> $browser_version=$log_version[1];
>>> $browser_agent='IE';
>>> } elseif (ereg(
>>> 'Safari/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log _version))
>>> {
>>> $browser_version=$log_version[1];
>>> $browser_agent='Safari';
>>> } elseif (ereg(
>>> 'Opera/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_ version))
>>> {
>>> $browser_version=$log_version[1];
>>> $browser_agent='Opera';
>>> } elseif (ereg(
>>> 'Mozilla/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$lo g_version))
>>> {
>>> $browser_version=$log_version[1];
>>> $browser_agent='Mozilla';
>>> } else {
>>> $browser_version=0;
>>> $browser_agent='Other';
>>> }
>>>
>>> return $browser_agent;
>>> }
>>>
>>> echo getBrowserAgent();
>>> ?>
>>>
>>> good luck,
>>>
>>> NN
>>
>>
>>The only problem is not all browser reports are accurate and not all
>>browsers of the same family support the same css - or anything else for that
>>matter - the same way.
>>
>>ken
>>
> you are right. the traditional method of using javascript is not
> accurate either because there are many people who turn off JS
> alltogether.
>
> on the function above you could also use the browser_version and also
> add a function to determine the platform, but again it's not always
> perfect. i've had problems myself with Opera, even though it fully
> supports CSS, etc; and of course i've had major headackes with IE6 &
> IE7
>
> here is what i use to determine the platform:
>
> if (strstr($_SERVER['HTTP_USER_AGENT'],'Win')) {
> $browser_platform='Windows';
> } else if (strstr($_SERVER['HTTP_USER_AGENT'],'Mac')) {
> $browser_platform='Mac';
> } else if (strstr($_SERVER['HTTP_USER_AGENT'],'Linux')) {
> $browser_platform='Linux';
> } else if (strstr($_SERVER['HTTP_USER_AGENT'],'Unix')) {
> $browser_platform='Unix';
> } else {
> $browser_platform='Other';
> }
>
> NN

I seem to recall, years ago, there was a browsercap.ini file that contained
the various abilities of a number of browsers and differenet versions and
sub versions of the same browser. You'd parse the user agent info and
compare to the ini file then serve up the page accordingly....

I haven't seen it lately.

ken

Re: using different stylesheets for different browsers (how to)?

am 09.04.2008 23:48:26 von nn

On Wed, 09 Apr 2008 16:20:16 -0500, No_One
wrote:

>On 2008-04-09, NN wrote:
>> On Wed, 09 Apr 2008 14:19:27 -0500, No_One
>> wrote:
>>
>>>On 2008-04-09, NN wrote:
>>>>
>>>> function getBrowserAgent(){
>>>>
>>>> if (ereg( 'MSIE
>>>> ([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version )) {
>>>> $browser_version=$log_version[1];
>>>> $browser_agent='IE';
>>>> } elseif (ereg(
>>>> 'Safari/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log _version))
>>>> {
>>>> $browser_version=$log_version[1];
>>>> $browser_agent='Safari';
>>>> } elseif (ereg(
>>>> 'Opera/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_ version))
>>>> {
>>>> $browser_version=$log_version[1];
>>>> $browser_agent='Opera';
>>>> } elseif (ereg(
>>>> 'Mozilla/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$lo g_version))
>>>> {
>>>> $browser_version=$log_version[1];
>>>> $browser_agent='Mozilla';
>>>> } else {
>>>> $browser_version=0;
>>>> $browser_agent='Other';
>>>> }
>>>>
>>>> return $browser_agent;
>>>> }
>>>>
>>>> echo getBrowserAgent();
>>>> ?>
>>>>
>>>> good luck,
>>>>
>>>> NN
>>>
>>>
>>>The only problem is not all browser reports are accurate and not all
>>>browsers of the same family support the same css - or anything else for that
>>>matter - the same way.
>>>
>>>ken
>>>
>> you are right. the traditional method of using javascript is not
>> accurate either because there are many people who turn off JS
>> alltogether.
>>
>> on the function above you could also use the browser_version and also
>> add a function to determine the platform, but again it's not always
>> perfect. i've had problems myself with Opera, even though it fully
>> supports CSS, etc; and of course i've had major headackes with IE6 &
>> IE7
>>
>> here is what i use to determine the platform:
>>
>> if (strstr($_SERVER['HTTP_USER_AGENT'],'Win')) {
>> $browser_platform='Windows';
>> } else if (strstr($_SERVER['HTTP_USER_AGENT'],'Mac')) {
>> $browser_platform='Mac';
>> } else if (strstr($_SERVER['HTTP_USER_AGENT'],'Linux')) {
>> $browser_platform='Linux';
>> } else if (strstr($_SERVER['HTTP_USER_AGENT'],'Unix')) {
>> $browser_platform='Unix';
>> } else {
>> $browser_platform='Other';
>> }
>>
>> NN
>
>I seem to recall, years ago, there was a browsercap.ini file that contained
>the various abilities of a number of browsers and differenet versions and
>sub versions of the same browser. You'd parse the user agent info and
>compare to the ini file then serve up the page accordingly....
>
>I haven't seen it lately.
>
>ken

yes, you are right.
here is the link to the php_browscap.ini. (there is even a lite
version)
http://browsers.garykeith.com/downloads.asp

NN

Re: using different stylesheets for different browsers (how to)?

am 10.04.2008 00:20:21 von No_One

>
> yes, you are right.
> here is the link to the php_browscap.ini. (there is even a lite
> version)
> http://browsers.garykeith.com/downloads.asp
>
> NN

Yeah, that's it. I googled for it after I dropped my message and the
newest I could find was dated 2004.

Thanks for the link.

ken

Re: using different stylesheets for different browsers (how to)?

am 12.04.2008 00:14:47 von Michael Fesser

..oO(Ronald Raygun)

>I have spent the last few days trying to create a tableless web page,
>using CSS only for layout. I have finally given up. My current css file
>works fine for IE - but looks terrible in Netsacpe and Firefox. if I fix
>the issue so that it works for Netscape/Firefox, then the page looks
>like crap in IE.

Write standards-compliant code and CSS for real browsers, then if
necessary add an IE-specific CSS within a conditional comment.
That's all. No need for browser sniffing.

Micha