Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 248
Industrial Avatar asked Mar 24 '10 14:03

Industrial


1 Answers

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

like image 155
Giovanni Di Milia Avatar answered Sep 18 '22 17:09

Giovanni Di Milia