tags:

views:

226

answers:

5

Im working with PHP 4.3.11 and when I execute a header always responds with an error like this

Warning: Cannot modify header information - headers already sent by (output started at d:\folder\file.php:1) in d:\folder\file.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at d:\folder\file.php:1) in d:\folder\file.php on line 3 Current PHP version: 4.3.11

the code I used to generate this error was

<?php
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");   // Date in the past

    echo 'Current PHP version: ' . phpversion();

    // prints e.g. '2.0' or nothing if the extension isn't enabled
    echo phpversion('tidy');
?>

It has no spaces nor newlines before or after the php tags, and the same code in a 5.x version returns just the php version as expected.

Any clue?

Thanks in advance

Edit: Solved!: I've opened the file with western european encoding and deleted the BOM and it worked. Thanks all for your help!

+5  A: 

Make sure that there are no empty lines and invisible characters (such as the UTF BOM) before the PHP block so that <?php is really the first in the file.

Gumbo
thanks for your fast response, but it doesnt, its really making me crazy :|
Eugenio Miró
Have you tried a hex editor to check the latter?
Gumbo
+4  A: 

Do you have a UTF BOM at the start of the file?

Greg
yes, and also in 5.x version
Eugenio Miró
Thanks! I've checked with another editor and dropped those chars and it worked !!!
Eugenio Miró
A: 

The error you're getting means that your entire script (other files included) are sending some output before the header() function is called.

You should revise all your files and see if any of them is outputting something (with echo() or print()), or if you missed some empty spaces after the last closing tag ?>

Seb
the error is produced even if I execute the file directly, other executions should return the same because they're made using ajax.
Eugenio Miró
Like @solomongaby says, you should skip the closing tag entirely. It is allowed in PHP, and doing so lets you avoid problems with trailing whitespace/linefeed
Jukka Dahlbom
+2  A: 

if your file does not contain anything else but php code it is recommended to skip the php closing tag to avoid empty spaces issue at the end of the file

solomongaby
done with same results, thanks!
Eugenio Miró
A: 

it's unlikely, but there's the possibility to define auto_prepend_file in the php.ini (probably .htaccess too).

http://at2.php.net/manual/en/ini.core.php - this acts like a require() at the beginning of every script. you can check this by looking at the phpinfo(); output.

but i too think the problem is the utf-8 BOM.

Schnalle