I'd like to wrap a css file in php... So I write the header for the css file and give it a php extension, thus... css.php. The question is will this work if the page is already being used as an include... or will this new header clash with the frame the page is being included into????
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
gnarf nailed it.
I do:
and then in top of your .css.php file:
HTML file:
<link rel="stylesheet" type="text/css" href="css.php"/>
css.php file:
<?php
header("Content-type: text/css; charset=utf-8");
//your php + css code goes here
?>
If your FIRST line of code in your CSS.php file is not
header("Content-type: text/css; charset=utf-8");
it will not workIt will works if the includer script do not send any output, otherwise you will have an "Headers already sent" error.
using ob_start() in the includer can be a trick to avoid this and have everything work.
Html frames have nothing to do with php includes instead, so no conflict at all in this case.
If you have a file called
css.php
just make sure the first lines set the proper content-type header. You may also want to split your session setup stuff (if there is any) into abootstrap.php
if you haven't already. A quick example of loading some style info from a databse:From your other php file, just output the tag to include the css.php, you do not want to use the php
include()
function for this task!Although since most browsers will cache your css file pretty aggressively, you might find that dynamically changing the contents of that file doesn't do much good. You could force that to update by adding a get parameter to the href of the link like so:
Although this is going to completely reload your css file every time that parameter changes. It is generally better practice to serve up static css files for this reason. If you have some styles that need to be loaded from configuration parameters, etc, that don't change very often, the first example should work for you quite well.