1

I need help with redirecting using header.

My code:

<?php

if($fname != "") {
    $query = mysql_query("UPDATE customer_address SET first_name='$fname',last_name='$lname',company='$company',company_id='$company_id',
                                address_1='$address_1',address_2='$address_2',city='$city',county='$county', post_code='$postcode',country='$country'
                                            WHERE address_id='$editThisId';");

    if($query==true) {
        header('Location: address.php');
    } else {
        echo "Update Error!";
    }
}

And I get the following error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\eula\edit-address.php:72) in C:\xampp\htdocs\eula\edit-address.php on line 129

Pastebin for whole file: http://pastebin.com/vj2Mp7u0

Thanks in advance!

user1978142
  • 7,946
  • 3
  • 17
  • 20
Nemesis429
  • 13
  • 3

4 Answers4

1

You are getting this error because your server already sent information to the client's browser - Headers need to be sent first before any HTML is transferred.

Simply put the code which is supposed to redirect the user at the very top of your file before any HTML or echo-calls.

Alternatively, you can call ob_start() at the top of your file to disable output buffering and send the page as a whole after every bit of your PHP code has executed.

Jan Berktold
  • 976
  • 1
  • 11
  • 33
0

You can not have any html content being displayed before the header, example:

<html>
...
<?php 
 // your code
 header('Location: address.php');
?>

Try putting the php code, before any html content.

abfurlan
  • 415
  • 5
  • 14
0

"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file."

from http://www.php.net/manual/en/function.header.php

Sirius_Black
  • 471
  • 3
  • 11
0

You could also try moving your php to the top of the page (before any of the HTML) instead of the bottom. That way the php would look for if(isset($_POST['submit'])) before anything on the page is rendered.

Your redirects should work then I think.

John Detlefs
  • 952
  • 8
  • 16