i'm writing a redirect.php page for a CMS. the admin user makes whatever changes they want to certain information in the database, then clicks submit. they are taken to redirect.php which handles all the sql commands depending on what the admin wanted to change. and then they are redirected to another page, depending on the types of changes they made, as well... the header is not working. here's the error:

Warning: Cannot modify header information - headers already sent by (output started at /../../../admin/redirect.php) in /../../../admin/redirect.php on line 41

<?php
    $uID        = $_POST['editID'];
    $sub_opt    = $_POST['sub_opt'];
    $brideFname = $_POST['brideFname'];
    $brideLname = $_POST['brideLname'];
    $groomFname = $_POST['groomFname'];
    $groomLname = $_POST['groomLname'];
    $ship_add   = $_POST['ship_add'];
    $ship_city  = $_POST['ship_city'];
    $ship_state = $_POST['ship_state'];
    $ship_zip   = $_POST['ship_zip'];
    $_POST['event_month'];
    $_POST['event_day'];
    $_POST['event_year'];
    
    if($_POST['event_month2'] != NULL){
        $event_month = $_POST['event_month2'];
    }if($_POST['event_day2'] != NULL){
        $event_day = $_POST['event_day2'];
    }if($_POST['event_year2'] != NULL){
        $event_year = $_POST['event_year2'];
    }

    if($sub_opt == "cancel"){
        echo "<CENTER>All changes were cancelled. You are being redirected to Home.</CENTER>";
        $redirect = "admin1.php?action=view_all";
    }elseif($sub_opt == "save"){
        //open connection
        //SQL commands
        //close connection
        echo "<CENTER>Your changes have been saved. You are being redirected to Home.";
        $redirect = "admin1.php?action=view_all";
    }elseif($sub_opt == "save_reg"){
        //open connection
        //same SQL commands
        //close connection
        echo "Your changes have been saved. You are being redirected to Registry.";
        $redirect = "updateRegistry.php";
    }

    header("Location: $redirect");
?>

if you are going to use a header tag YOU CANNOT output any text/html to the user, which means you need to get rid of all the echo.

when you see this error: Warning: Cannot modify header information - headers already sent by

it means that you've already send data to the user.
comment all the lines that contain echo and you will see your code should work just fine

what you could do to fix the problem is put this in the meta tag of the html:

<meta http-equiv="Refresh" content="4;url=http://www.domain.com/link.html">

you would need to replace the url with $redirect and the 4 seconds with however long you see fit

and it will redirect the user to the new page after 4 seconds

Warning: Cannot modify header information - headers already sent by (output started at /../../../admin/redirect.php) in /../../../admin/redirect.php on line 41

If you are facing this Warning....it means you have some ECHO syntaxes or you may have some waste spaces that are not allowed while using "header" function....Make sure tht u ve removed all ECHOs and waste spaces above the Header.... your function will work fine...

mmm, the delimiter between a HTTP header and the HTTP response is two line returns (one empty line). you can put aload of stuff before a header, but not an empty line... lol.. PHP tries to handle this automatically (in true CGI you have to send a content-type/location/[some other headers work], otherwise the request fails, if you use echo before you set a header, PHP sends a text/html content-type.)

So, as said, put header() before you do anything that could produce output.

The Location header is a request-level redirect (meaning a page that sends the Location header is never shown); so if you actually want to show something on the page during the redirect, maybe the Redirect header is more appropriate.

This article has been dead for over six months. Start a new discussion instead.