开发者

Safe to use dynamic HTML stylesheets using PHP?

开发者 https://www.devze.com 2023-01-19 13:18 出处:网络
ih ave read about the technique to allow dynamic HTML stylesheets using PHP\'s preprocessor, however I was windering how s开发者_如何学JAVAafe this tecnique is, and if for any reason it is discouraged

ih ave read about the technique to allow dynamic HTML stylesheets using PHP's preprocessor, however I was windering how s开发者_如何学JAVAafe this tecnique is, and if for any reason it is discouraged.

For example, instead of using typical:

 <link rel="stylesheet" type="text/css" href="http:mysite.com/style.css/>

I could use:

 <link rel="stylesheet" type="text/css" href="http:mysite.com/style.php/>

What do you think?


It's perfectly fine.

I'd suggest setting the following header in the PHP though:

Header("Content-Type: text/css");


If you can use .htaccess files you can set it up to parse *.css files as PHP using:

AddHandler application/x-httpd-php .css

Then you can use PHP directly within your *.css files.

Be sure to also set the header type to text/css as the others have mentioned as well:

header('Content-type: text/css');


It's only as safe as the code that you put into style.php.


Yes, you can use PHP to generate your stylesheet. Make sure to declare the output properly as CSS by sending an appropriate Content-Type value specifying both the media type and the character encoding.

But note that it costs additional time and resources to generate the stylesheet with every request. So you should add some kind of caching mechanism (static files and HTTP caching) to reduce server load and even unnecessary requests.


As others have mentioned, PHP can be used to output any kind of text. So it's not a problem to output dynamic CSS (or even dynamic JavaScript). Be aware though that you're increasing your server load by doing this. The server will have to fire up the PHP engine to serve what would otherwise be a simple static .css file.


You can render any kind of Text Output with PHP (and other stuff) including CSS, just make sure the right header is given header('Content-Type: text/css; charset=iso-8859-1'); (or any other charset).

But to be honest I can't think of an Situation were it would be necessary to use a dynamic stylesheet in that manner.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号