开发者

Content-type not working in PHP

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 */

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!

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

What can I do to sort this out?

UPDATE: Did change default_mimetype = "text/html" to default_mimetype = "text/css" in php.ini and all pages got immediately interpreted as css, s开发者_高级运维o 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; }';
    ?>

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

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

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


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.


  1. Make sure your file editor doesn't save a BOM in your PHP file.
  2. Try to move error_reporting / ini_set to make them the firsts PHP statements (before header() call). This way you will see all errors (if any). Don't forget to put that OFF in production!
  3. Silly remark, but make sure this file is interpreted as PHP (extension is .php or if not an .htaccess tell the server to interpret as PHP).
  4. Everything else is fine with your code. If it still doesn't work, check your server logs. Maybe something else crashes the execution of this PHP file (invalid MIME or else)...


the reason is because the header function works only if it is the first one to be called!

If you put an echo before, the content type automatically becomes text/html

try to print a CSS code after the header to test if it actually works.

Read this page for more infos

EDIT: did you change your post ? :-)


This is usually caused by a fatal error (ie, syntax error) that causes the script to abort before any of the code is execute (before display_errors can be set through ini_set() at runtime). Try changing display_errors in the php config file (php.ini).


Maybe the function header() is disabled in your configuration?

Test:

print ini_get('disable_functions');


It may be worth checking with curl to see what headers are actually being sent.

Try this from a command line and check for the "text/css":

curl -I http://example.com

Depending on the browser's request headers, PHP could also be sending the output gzipped using output buffering. In the PHP file, try this to check for ob_gzhandler.

print_r(ob_list_handlers());

If it's enabled, check in for zlib.output_compression in your php.ini or Apache configuration.


I found the full file is indented.

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

Because the indentation on line 1 outputted 4 spaces, therefore, the header will not work.


This sounds like your webserver is interpreting the script as a normal file. Does it have a .php extension and do other .php files work as expected?


Looks perfectly ok and the line with the echo should definitely generate a warning. Could it be that you're editing the wrong file?


You can try Content-Style-Type: text/css

See the below from the here

<META http-equiv="Content-Style-Type" content="text/css">

The default style sheet language may also be set with HTTP headers. The above META declaration is equivalent to the HTTP header:

Content-Style-Type: text/css

Edit:

At the link , it's mentionned to add AddType text/css .css in the apache config file. Maybe you can give it a try

Edit2 Look up for 'css' at this link. Someone had the same problem as you. Try sending the header without the charset

I recently had a hair-threatening problem with Firefox and XHTML 1.0 transitional.

It worked fine with other browsers, and also with HTML 4.1.

To cut a long story short, PHP-generated JS and CSS files were still being reported by the headers as text/html, while in the HTML they were text/css and application/javascript; Firefox having been told the page was XHTML 1.0 became anal-retentive and refused to style the page. (I think the JS still worked but I fixed it anyway.)

Solution:

header('Content-type: text/css'); and header('Content-type: application/javascript');

Edit 3 There was a post about some forms not submitting any data because of an utility called AVG Linkscanner. Since you have reinstalled Apache + php and I assume you didn't reinstall the OS, so you can maybe investigate on this/try by turning some utilities/plugs-ins off.


Wild guess : open your file with something which would display any BOM. If you see some strange characters before <?php you have your problem. Check your current editor options to save UTF-8 file and make it save them without BOM.


Maybe there are issues with caching.

Try this:

header('Content-type: text/css');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

echo 'body {background-color: #000000; }';

Works for me on an out of the box XAMPP installation and firefox - firebug reports correct content type.


output_buffering = Off in php.ini was the reason for me why it keeps sending Content-Type = text/html. Setting it to 1 solves it.


In my case, I struggled for hours with my code header('text/javascript');, wondering why the response MIME type wasn't being sent. The correct PHP code is header('Content-type: text/javascript');. There is no proper error detection for this programming error in PHP, Apache, the browsers, or the Web Developer Tools.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜