浏览器忽略php打印的css

浏览器忽略php打印的css

问题描述:

I have a stylesheet link that looks like below:

<link rel="stylesheet" href="/example/get_page.php?location=bla.css" id="main_ss" />

get_page.php just gets a URL using file_get_contents():

if (isset($_GET['location'])) {  
    echo file_get_contents('/example/styles/' . $_GET['location']);
}

I can see that the stylesheet file is being fetched properly (for example the text of the file is showing in firebug when I expend the link tag) but for some reason it is ignored by the browser. If I just fetch the CSS file directly of course everything works.

The code can be seen here: www.specman-verification.com/example/bla.html

Any leads? I'm at loss here.

Add the Content-type header like this (do this before you output anything):

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

Your code is just trying to load the script get_page.php. To load the CSS file you need:

<link rel="stylesheet" type="text/css" href="/example/bla.css" />

(or similar depending on the actual path to your CSS file). In other words the href attribute needs to specify the path to your spreadsheet file, not the HTML page file.

You need to do it the right way. I understand what you're doing here. You need a good mechanism to dynamically load external CSS and have the result display normal html in the browser output.

Follow the instructions on this url: http://www.warpconduit.net/2009/05/12/dynamically-load-css-and-js-files-using-php/

This will at least get you to have a mechanism to load external css file with php dynamically. You're definitely missing steps in your code.