内容类型在 PHP 中不起作用 [英] Content-type not working in PHP

查看:28
本文介绍了内容类型在 PHP 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些无法正常工作的 PHP 文件问题.Content-type 根本不会被任何浏览器接收.Firebug 将文件解释为 text/html 而不是 css.这是文件:

I have some issues with a PHP file that is not working properly. The Content-type does not get recieved by any browser at all. Firebug interprets the file as text/html instead of css. Here's the file :

<?php
header('Content-Type: text/css; charset=UTF-8');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
/* CSS goes on from here */

我测试在标题行之前放置一行 echo 'TEST';,并希望看到经典的标题已发送"错误,但什么也没出现!

I tested to put a row with echo 'TEST'; before the header line, and was expecting to see the classic "headers already sent" error, but nothing appears!

然而,普通的 .css 文件就像一个魅力.

Normal .css-files are working like a charm however.

我能做些什么来解决这个问题?

What can I do to sort this out?

更新:是否在 php.ini 中将 default_mimetype = "text/html" 更改为 default_mimetype = "text/css" 并且所有页面都立即被解释为 css,所以必须有一种方法只发送此文件的 css 标头 :)

UPDATE: Did change default_mimetype = "text/html" to default_mimetype = "text/css" in php.ini and all pages got immediately interpreted as css, so there's must be a way to just send css headers for this file :)

来自约翰需求的完整文件:

The full file from demand of John:

    <?php
    header('Content-Type: text/css; charset=UTF-8');
    echo 'body {background-color: #000000; }';
    ?>

更新 #2:在 PHP 文件中添加 ini_set('default_mimetype', 'text/css'); 修复了这个文件,但并没有解决导致这个错误的问题...

UPDATE #2: Adding ini_set('default_mimetype', 'text/css'); to the PHP file fixes this file, but it doesn't solve the issue that causes this fault...

更新 #3:测试将 AddType text/css .css 添加到 .htaccess 和 Apache 配置.仍然没有运气.还测试发送与字符集分开的标头:header('Content-Type: text/css'); - 仍然没有运气...

UPDATE #3: Tested adding AddType text/css .css to both .htaccess and Apache config. Still no luck. Also tested to send headers separated from charset: header('Content-Type: text/css'); - Still no luck...

更新 #4:已在服务器上重新安装 Apache+PHP 以查看问题是否消失,但没有.老样子,老样子……

UPDATE #4: Have reinstalled Apache+PHP at the server to see if the problem goes away, but no. Same old, same old...

推荐答案

检查 php.ini 文件中的 output_buffering 设置.如果它没有设置为关闭",那么 PHP 会自动为您进行输出缓冲.将其设置为关闭并在标题命令之前回显某些内容,您应该会看到经典错误".

Check your php.ini file for the output_buffering setting. If it's not set to "off" than PHP is automatically doing output buffering for you. Set that to off and echo something before the header command, and you should see the "classic error".

你不应该使用结束语?>.我知道这是一个有争议的建议,但是很多时候人们在它后面添加一个返回和/或空格,这会输出到浏览器(在标题之前).很少有不使用它会导致问题的情况.

You shouldn't use the closing ?>. I know this is a controversial suggestion, but too many times people add a return and/or space after it, which gets output to the browser (before the header). There are very few cases where it not using it would cause a problem.

这篇关于内容类型在 PHP 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆