External stylesheets

S

SeeSchloss

Hi,

I'd like to dynamically generate an external stylesheet
with PHP. Therefore, I replaced the
<link rel="stylesheet" type="text/css" href="style.css" />
with
<link rel="stylesheet" type="text/css" href="style.php" />
(replaced .css with .php).
The problem is that the browser only seems to use the
stylesheet when the extension is '.css', I also tried
'.inc' and a few others, and only '.css' worked (the files
were exactly the same, of course). And since .css files are
not interpreted by PHP (without modifying the config
file)...
Is it normal ? And could you suggest any other way to do it
?
I could make it internal, but since it is about 5KB, it
would almost double the weight of my pages.

Thanks.
 
D

David Dorward

SeeSchloss said:
I'd like to dynamically generate an external stylesheet
with PHP. Therefore, I replaced the
<link rel="stylesheet" type="text/css" href="style.css" />
with
<link rel="stylesheet" type="text/css" href="style.php" />
The problem is that the browser only seems to use the
stylesheet when the extension is '.css'

File extensions are irrelevant, its the content type in the http header that
browsers (should[1]) respect for deciding what type of file it is. By
default PHP outputs files with a text/html header (which is sensible given
that almost all PHP files are designed to output HTML documents). You have
to explicitly tell it to send a different content type header if you don't
want that.

<?php header('Content-type: text/css'); ?>


[1] Internet Explorer sometimes second guesses the author, I don't know if
file extension plays a role in this.
 
S

SeeSchloss

David Dorward a écrit :
SeeSchloss said:
I'd like to dynamically generate an external stylesheet
with PHP. Therefore, I replaced the
<link rel="stylesheet" type="text/css" href="style.css" />
with
<link rel="stylesheet" type="text/css" href="style.php" />
The problem is that the browser only seems to use the
stylesheet when the extension is '.css'

File extensions are irrelevant, its the content type in the http
header that browsers (should[1]) respect for deciding what type
of file it is. By default PHP outputs files with a text/html
header (which is sensible given that almost all PHP files are
designed to output HTML documents). You have to explicitly tell
it to send a different content type header if you don't want
that.

<?php header('Content-type: text/css'); ?>


[1] Internet Explorer sometimes second guesses the author, I
don't know if file extension plays a role in this.

Ok ok, it works now. Since all I did was changing the
extension without adding any code to it, I thought it
should have worked to same, but the file goes through php
anyway.

By the way, I forgot to say it but the problem was with
Mozilla Firebird, I just tried with Internet Explorer and
it worked even without the text/css header.

Thanks for the quick answer :)
 
D

David Dorward

SeeSchloss said:
By the way, I forgot to say it but the problem was with
Mozilla Firebird, I just tried with Internet Explorer and
it worked even without the text/css header.

Internet Explorer: This style sheet is written in text/html, bah I'll try to
treat it as CSS.

Firebird: This style sheet is written in text/html, I don't know how to
handle this, so I'll ignore it.

I much prefer Firebirds approach - if at some stage a different style sheet
language was created, that had similar syntax to CSS, IE would probably try
to treat it as CSS and get things very very wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top