header(\"Content-type: text/css\");
works in Firefox, Chrome and other, but not in Internet Explorer 9. I am not sure what\'s up.
In Chrome an
I think the problem maybe due to mod_security
which is serving a 401 error page (HTML) rather than the CSS. Try adding this to an .htaccess
file in the web root of your website to see if this resolves the issue.
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
The article Handle Images/CSS/JS as PHP without httpd.conf Using .htaccess helped me figure out the issue.
You basically do all the same stuff as everyone else mentioned (setting header in PHP, etc.), but also use .htaccess
to set the AddHandler param.
<Files my_style.css>
ForceType application/x-httpd-php
AddHandler application/x-httpd-php .css
</Files>
This way you can name it with .css and still have PHP parse it.
IE has "No, I'm not kidding about Content-Type" switch:
X-Content-Type-Options: nosniff
BTW: make sure you also send Last-Modified
and disable session.cache_limiter
in PHP, otherwise browsers will keep reloading the CSS file, which will negatively impact performance.
Internet Explorer has a history of trusting the file extension over the MIME type reported by the browser. If you have mod_rewrite available, have your HTML look for a .css file and then create a mod_rewrite rule that pipes that URL to your script.
I have found that using header("Content-type: text/css", true);
works for me. It prevents the server from outputing 2 HTTP headers for 'Content-Type'.
Recently I had a problem like this for myself too. Nasty debugging is all I can tell about this. Not sure if this might help you people, but I hope it will. Cut to the chase.
Internet Explorer uses something called Mime-type sniffing, which simply reads 200 Bytes of THE FILE header.
I had a PHP script which had to return different video file (with data - the whole file) dependent on the parameter it had been supplied with.
My video request example: get-video.php?id=here_goes_the_id
$file = ''; // Get the video file path
if (is_file($file)) {
header("Content-Type: video/mp4");
header("Content-Length: " . filesize($file));
readfile($file);
}
exit(); // This is the turnaround
You have to suspend your script immediately after it sends the file data. This prevents the IE Mime-type sniffing.
Hope it helps.