简体   繁体   English

header(“Content-type:text / css”);正在使用Firefox和Chrome,但在Internet Explorer 9中它显示为“text / html”

[英]header(“Content-type: text/css”); is working in Firefox and Chrome, but in Internet Explorer 9 it shows up as 'text/html'

header("Content-type: text/css"); works in Firefox, Chrome and other, but not in Internet Explorer 9. I am not sure what's up. 适用于Firefox,Chrome等,但不适用于Internet Explorer 9.我不确定是什么。

In Chrome and Firework it shows the style sheet if I open it in its own tab and it's being applied to the page. 在Chrome和Firework中,如果我在自己的标签中打开它并将其应用于页面,则会显示样式表。

In Chrome under Network in the developer tools it says the type is text/css and the status is 200. 在开发人员工具的网络下的Chrome中,它表示类型为text/css ,状态为200。

In Internet Explorer 9, it wants to download the style sheet if I open it in its own tab and it's not being applied to the page. 在Internet Explorer 9中,如果我在自己的选项卡中打开它并且它没有应用于页面,则它想要下载样式表。

In the F12 developer tools you can click on network, start capturing and refresh the page. 在F12开发人员工具中,您可以单击网络,开始捕获并刷新页面。 It shows the Style.css.php . 它显示了Style.css.php The type is text/html and the result is 406. 类型是text/html ,结果是406。

This is in the head: 这是在头脑中:

<link rel="stylesheet" type="text/css" href="/assets/css/style.css.php" media="screen" />

Request headers: 请求标头:

Key Value
Request GET /assets/css/main.css HTTP/1.1
Accept  text/css
Referer http://10.0.1.5/
Accept-Language en-US
User-Agent  Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding gzip, deflate
Host    10.0.1.5
Connection  Keep-Alive
Cookie  PHPSESSID=*Hidden*

Response headers: 响应标头:

Key Value
Response    HTTP/1.1 406 Not Acceptable
Date    Fri, 01 Apr 2011 10:12:42 GMT
Server  Apache/2.2.14 (Ubuntu)
Alternates  {"main.css.php" 1 {type application/x-httpd-php}}
Vary    negotiate
TCN list
Keep-Alive  timeout=15, max=100
Connection  Keep-Alive
Content-Type    text/html; charset=iso-8859-1

IE has " No, I'm not kidding about Content-Type " switch: IE有“ 不,我不是在开玩笑式内容类型 ”开关:

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. 顺便说一句:确保你也发送Last-Modified并在PHP中禁用session.cache_limiter ,否则浏览器将继续重新加载CSS文件,这将对性能产生负面影响。

Internet Explorer has a history of trusting the file extension over the MIME type reported by the browser. Internet Explorer具有通过浏览器报告的MIME类型信任文件扩展名的历史记录。 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. 如果您有mod_rewrite可用,请让您的HTML查找.css文件,然后创建一个mod_rewrite规则,将该URL传递给您的脚本。

I have found that using header("Content-type: text/css", true); 我发现使用header("Content-type: text/css", true); works for me. 适合我。 It prevents the server from outputing 2 HTTP headers for 'Content-Type'. 它可以防止服务器为“Content-Type”输出2个HTTP标头。

Is there any output before the header is sent? 在发送标头之前是否有任何输出? BOM in the php file? 在php文件中的BOM? A carriage return? 回车?

If you disabled error reporting, you may not see the error it should trigger. 如果您禁用了错误报告,则可能看不到它应该触发的错误。

Try adding ob_start() on top, it will resolve any issue related to headers already sent before any header() call. 尝试在顶部添加ob_start() ,它将解决与任何header()调用之前已发送的标头相关的任何问题。

If you have a BOM in your UTF8 file, you may want to remove it. 如果您的UTF8文件中有BOM,则可能需要将其删除。

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. Internet Explorer使用称为Mime类型的嗅探,它只读取200字节的THE FILE标题。

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. 我有一个PHP脚本,它必须返回不同的视频文件(包含数据 - 整个文件),这取决于它提供的参数。

My video request example: get-video.php?id=here_goes_the_id 我的视频请求示例: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. 这可以防止IE Mime类型的嗅探。

Hope it helps. 希望能帮助到你。

I think the problem maybe due to mod_security which is serving a 401 error page (HTML) rather than the CSS. 我认为问题可能是由于mod_security服务于401错误页面(HTML)而不是CSS。 Try adding this to an .htaccess file in the web root of your website to see if this resolves the issue. 尝试将此添加到您网站的Web根目录中的.htaccess文件,以查看是否可以解决问题。

<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. 文章Handle Images / CSS / JS as PHP without httpd.conf使用.htaccess帮助我找出问题所在。

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. 你基本上做了与其他人提到的相同的东西(在PHP中设置标题等),但也使用.htaccess来设置AddHandler参数。

<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. 这样你就可以用.css命名它,并且仍然用PHP解析它。

For Internet Explorer 9 serving HTTP 406 status code in the response header for a dynamically generated CSS file we: 对于在动态生成的CSS文件的响应头中提供HTTP 406状态代码的Internet Explorer 9,我们:

  1. Removed the Apache mod_negiotiation module or added -Multiviews to the host configuration (or .htaccess). 删除了Apache mod_negiotiation模块或将-Multiviews添加到主机配置(或.htaccess)。
  2. Activated Apache mod_rewrite 激活的Apache mod_rewrite
  3. Added a rewrite rule to the virtual host configuration or htaccess: 为虚拟主机配置或htaccess添加了重写规则:

     <IfModule mod_rewrite.c RewriteEngine on RewriteBase / #serve CSS and JS from combine.php RewriteRule ^combine(.*) combine.php [NC,L] </IfModule> 

Note: This solution is only good for servers/virtual-hosts not using Multiviews as removing mod_negotiation removes usage of this directive as well. 注意:此解决方案仅适用于不使用Multiviews的服务器/虚拟主机,因为删除mod_negotiation也会删除此指令的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM