php header("内容类型:文本/css"); 在 Firefox 和 Chrome 中工作,但在 Internet Explorer 9 中显示为“text/html”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5413107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:19:30  来源:igfitidea点击:

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

phphttp-headerscontent-typeinternet-explorer-9

提问by Keverw

header("Content-type: text/css");works in Firefox, Chrome and other, but not in Internet Explorer 9. I am not sure what's up.

header("Content-type: text/css");适用于 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/cssand the status is 200.

在 Chrome 的开发者工具中的 Network 下,它说类型text/css是 200,状态是 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. It shows the Style.css.php. The type is text/htmland the result is 406.

在 F12 开发者工具中,您可以单击网络,开始捕获并刷新页面。它显示Style.css.php. 类型是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

回答by Kornel

IE has "No, I'm not kidding about Content-Type" switch:

IE 有“不,我不是在开玩笑 Content-Type”开关:

X-Content-Type-Options: nosniff

BTW: make sure you also send Last-Modifiedand disable session.cache_limiterin PHP, otherwise browsers will keep reloading the CSS file, which will negatively impact performance.

顺便说一句:确保您也在PHP 中发送Last-Modified和禁用session.cache_limiter,否则浏览器将不断重新加载 CSS 文件,这将对性能产生负面影响。

回答by Nathan Strong

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.

Internet Explorer 有过信任文件扩展名而不是浏览器报告的 MIME 类型的历史。如果您有可用的 mod_rewrite,请让您的 HTML 查找 .css 文件,然后创建一个 mod_rewrite 规则,将该 URL 传送到您的脚本。

回答by samshull

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'.

我发现使用header("Content-type: text/css", true);对我有用。它可以防止服务器为“Content-Type”输出 2 个 HTTP 标头。

回答by Capsule

Is there any output before the header is sent? BOM in the php file? A carriage return?

在发送标头之前是否有任何输出?php文件中的BOM?回车?

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,您可能需要将其删除。

回答by Marcel

I think the problem maybe due to mod_securitywhich is serving a 401 error page (HTML) rather than the CSS. Try adding this to an .htaccessfile in the web root of your website to see if this resolves the issue.

我认为问题可能是由于mod_security哪个提供 401 错误页面 (HTML) 而不是 CSS。尝试将其添加到.htaccess您网站的 Web 根目录中的文件中,看看是否能解决问题。

<IfModule mod_security.c>
    SecFilterEngine Off
    SecFilterScanPOST Off
</IfModule>

回答by Mike

The article Handle Images/CSS/JS as PHP without httpd.conf Using .htaccesshelped me figure out the issue.

文章将图像/CSS/JS 作为 PHP 处理而不使用 httpd.conf 使用 .htaccess帮助我解决了这个问题。

You basically do all the same stuff as everyone else mentioned (setting header in PHP, etc.), but also use .htaccessto 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 解析它。

回答by Paktas

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 -Multiviewsto the host configuration (or .htaccess).
  2. Activated Apache mod_rewrite
  3. Added a rewrite rule to the virtual host configuration or htaccess:

    <IfModule mod_rewrite.c
    
      RewriteEngine on
      RewriteBase /
    
      #serve CSS and JS from combine.php
      RewriteRule ^combine(.*) combine.php [NC,L]
    
    </IfModule>
    
  1. 删除了 Apache mod_negiotiation 模块或将-Multiviews添加到主机配置(或 .htaccess)。
  2. 激活 Apache mod_rewrite
  3. 向虚拟主机配置或 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 也会删除此指令的使用。

回答by Lukas

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.

希望能帮助到你。