views:

430

answers:

12

Hi everyone,

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, 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; }';
    ?>

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

+1  A: 

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 ? :-)

Giovanni Di Milia
Hi man! Yep, edited the post to make it a bit clearer. Sorry!
Industrial
UPDATE: try to override the `default_mimetype` of the php.ini from the code...
Giovanni Di Milia
Hi Giovanni, that worked for this file! Thanks!
Industrial
A: 

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?

ar
Hi! It sure does. Everything else is working like a charm and Mod_rewrite is enabled as well!
Industrial
A: 

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

soulmerge
I'm afraid not, thats the first in my checklist to try :)
Industrial
+1  A: 

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

John Himmelman
Actually cleaned out the whole file, and just had the header line there. It still doesn't want to interpret as CSS.
Industrial
Post the css, something is causing a fatal error. Which is why the echo statement at the bottom didn't do anything and php errors aren't being displayed. There could be something in your css that is breaking the script (especially true if you're echo'ing it, its easy to miss an unescaped quote)
John Himmelman
Hi John, posted up the full file in the first post, even though it's just one line of Css that should be valid...
Industrial
A: 

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.

ccheneson
Nope! Didn't change anything I'm afraid :(
Industrial
Industrial
Hi again. Tried to send just the content-type without any charset earlier. Nothing new. Thanks for your help!
Industrial
The "default style sheet language" applies to the style **attribute**, style elements take their language from the required type attribute, linked stylesheets take theirs from the HTTP header.
David Dorward
+1  A: 

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

Test:

print ini_get('disable_functions');
toscho
A: 

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.

dave1010
A: 

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.

Arkh
A: 

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.

zaf
+2  A: 
  1. Make sure your file editor don'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 don't work, check your server logs. Maybe something else crash the execution of this PHP file (invalid MIME or else)...
AlexV
Hi! Nothing in the server logs. I'll try your first tstep now and get back. Thanks
Industrial
+1  A: 

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.

SHiNKiROU
+1  A: 

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.

Brent Baisley