-6

Possible Duplicate:
Headers already sent by PHP

I am getting the following error from the following code, and I am not entirely sure why. If you could tell me how to fix it, that would be great. Thanks in advanced.

Warning: Cannot modify header information - headers already sent by (output started at...) on line 45.

<?php


    // Initialization
    $conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
    mysql_select_db(DB_NAME, $conn);

    // Error checking
    if(!$conn) {
        die('Could not connect ' . mysql_error());
    }

    // Localize the GET variables
    $ref  = isset($_GET['ref']) ? $_GET['ref'] : "";

    // Protect against sql injections
    // Insert the score
    $retval = mysql_query("INSERT INTO $table(
            site
        ) VALUES (
            '$ref'
        )",$conn);

    if($retval) {
        echo "Successfull";
    } else {
        echo "Unsuccessfull " . mysql_error();
    }

    mysql_close($conn);
?>
<?php
$url = $_GET['url'];
    $loc = 'Location: '. $url;
    header($loc);
exit;
?>
Community
  • 1
  • 1
max_
  • 24,076
  • 39
  • 122
  • 211
  • 5
    http://stackoverflow.com/search?q=Cannot%20modify%20header%20%20information or http://www.google.com/search?q=cannot+modify+header+information – mario Apr 11 '11 at 18:46

5 Answers5

6

Take out the echo calls, you can't send information to the browser before the headers.

You can try something like this to still show if an error happens:

if(!$retval) {
    echo "Unsuccessfull " . mysql_error();
}
DShook
  • 14,833
  • 9
  • 45
  • 55
1

If you change the headers you cannot output any text prior to to the header command otherwise the headers will already be sent.

ie.

if($retval) {
    echo "Successfull";
} else {
    echo "Unsuccessfull " . mysql_error();
}

Is outputting text before you change the headers.

GWW
  • 43,129
  • 11
  • 115
  • 108
0

Use:

<meta http-equiv="Refresh" content="0;url=http://www.example.com" />
Kijewski
  • 25,517
  • 12
  • 101
  • 143
KSA dev
  • 51
  • 2
0

Use Output Buffers: http://php.net/manual/en/function.ob-start.php

ob_start();

at the start and

ob_end_flush();

at the end.

gmadd
  • 1,146
  • 9
  • 18
0

What I generally recommend for situations like this, is save all output to the end, as gmadd mentioned, you can do the ob_start, but I prefer to store the data in a string without having to add the extra code (I know you can also designate this in the .htaccess file, I would go that route over adding the actual ob_start items).

What I would do:

$display = ""; // initiate the display string 
// etc doe here
if($retval) {
    $display .= "Successfull";
} else {
    $display .= "Unsuccessfull " . mysql_error();
}

// end of the script right before ?>
echo $display;
?>

The ob_start method works and if you want to go that route, you can add this in the .htaccess file (given that allowoverride is set in your apache setup):

php_value output_buffering On

Again I still recommend the $display storage method, but that is my personal opinion.

Jim
  • 18,673
  • 5
  • 49
  • 65