tags:

views:

63

answers:

8
+2  Q: 

PHP Header Problem

My code is no white spaces before or afther

<?php
ob_start();
header("content-type:text/xml; charset=UTF-8");

function getXML($sql="Default Query")
{
    $conn=mysql_connect("*","*","*");
    $db=mysql_select_db("*");
    $result = mysql_query($sql,$conn);
    $columns="";
    echo "<records>";
    while($row=mysql_fetch_assoc($result))
    {
        $columns.="<record>";
        foreach($row as $key => $value)
        {
            $reptxt = str_replace("&", "&amp;", $value);
            $columns.="<$key>$reptxt</$key>";
        }
        $columns.="</record>";
    }
    echo $columns;
    echo "</records>";
}
getXML("SELECT 0,1,4,5 FROM KK_VOISTLEJAD");
ob_end_flush();
?>

And error what i get is: Warning: Cannot modify header information - headers already sent by (output started at .../htdocs/data.php:1) in .../htdocs/data.php on line 2

Whats wrong?

+3  A: 

You only can modify the header information in PHP when nothing has been outputed yet. Else the headers are send already and you get this message.

Use buffering techniques see ob_start() and friends to prevent outputting stuff and thus still be able to send headers.

Veger
A: 

You have to make sure you don't send any output (e.g. by using echo) before you call header().

echo
A: 

Check for whitespaces or new lines before

<?php 

or after

 ?> 

I bet it is this your problem.

Eineki
+1  A: 

Probably the problem is UTF BOM. But just type error contents into Google - you'll get thousands of answers... it's a really famous problem for beginners.

Crozin
A: 

In addition to the things that others said, make sure that you don't have too many line breaks after your closing ?> tag, or just omit it altogether (it isn't necessary).

Andrew Noyes
It is good coding style to finish any block you started. So omitting `?>` is a wrong habit.
Veger
Most coding standards (Zend, for example) require that you omit the closing ?>. It doesn't delimit a block of code, it's just there to allow you to write spaghetti code. `<?php` and `?>` shouldn't be thought of as tags. Since the file doesn't output any HTML to the browser, you're better off omitting it since you'll never have a "Headers already sent" issue like this one.
Andrew Noyes
True, it support spaghetti code and that is certainly bad. But it surely delimits a block: In this case it delimits a PHP code block and returns to the the HTML/text/whatever 'environment'.
Veger
Which is what we don't want. The only reason to delimit the segment of PHP code is if you're going to follow it with HTML. Since this file is pure PHP code, there's no reason to end the file with the closing delimiter, which can cause problems (like this one).
Andrew Noyes
A: 

You can use output buffering to avoid this problem. http://us3.php.net/manual/en/function.ob-start.php

Put this at the beginning of your script (before the call to header()):

ob_start();

and then put this at the end of your script (after the last output statement):

ob_end_flush();
pib
A: 

You have to be very careful when modifying headers. If you have even 1 white space before your php tag then you'll get an error, this rule is also true any files that are being included. For example this php will fail with a similar warning to the one you are getting:

 SINGLE_WHITE_SPACE<?php header("location: /");?>

You also have to keep track of any files that you include, if they have ANYTHING before or after the then you will not be able to modify headers or if they print or echo.

Rook
A: 

OK first thanks to all.. But i found the problem. When i changed my php file encoding from UTF-8 with signature to UTF-8 with out signature then there was no problem.

In my opinion we live in ........ world.

Woland