Content-type not working in PHP

Deadly 提交于 2019-11-28 08:40:44

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.

AlexV
  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?

ccheneson

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!