简体   繁体   中英

Why does php header redirect raise a warning?

Warning is raised when I use header (Location: ...) redirect in php why ?

this is the warning :

Warning: Cannot modify header information - headers already sent by (output started at ~\\index.php:51) in ~\\addMarket1.php on line 26

and this is the code :

$folder = 'images/markets/';
$orig_w = 500;

if( isset($_POST['submit']) )
{
    $imageFile = $_FILES['image']['tmp_name'];
    $filename = basename( $_FILES['image']['name']);

    if(strstr($_FILES["image"]["type"],"image")){
            list($width, $height) = getimagesize($imageFile);

            $src = imagecreatefromjpeg($imageFile);
            $orig_h = ($height/$width)* $orig_w;

            $tmp = imagecreatetruecolor($orig_w, $orig_h);
            imagecopyresampled($tmp, $src, 0,0,0,0,$orig_w,$orig_h,$width,$height);
            imagejpeg($tmp, $folder.$filename,100);

            imagedestroy($tmp);
            imagedestroy($src);

            $filename = urlencode($filename);
            mysql_query("insert into photos (`image`) values ('$filename')");   
        //  echo '<meta http-equiv="refresh" content="0;URL=../Admin/?p=crop&filename=$filename&height=$orig_h">'; 
        header("Location: ../Admin/crop.php?filename=$filename&height=$orig_h");
    }
    else
    echo "The file uploaded is not image. Please choose jpg, png,gif or png image !";
}

A redirection is made using an HTTP [Header] 2 (The Location one) .

HTTP headers can only be sent before the content is : when generating a page, the server will :

  • send the HTTP headers
  • then, send the body of the HTTP response

This is the way the HTTP protocol works -- nothing you can do about that.

If the body has started being sent, the HTTP headers have also already been sent -- and once that's done, you cannot send any HTTP header anymore -- which explains the warning.


Two possible solutions :

  • The best one : do not send any output (not even a single space) before trying to send HTTP headers
  • A bit hacky : Use output buffering , so the output is kept in RAM, and not sent to the browser -- which means HTTP headers can still be sent, even if the output has started being generated.

Note : you can use the headers_sent() function to determine whether HTTP headers have already been sent or not.

EDIT: His code is right. He is missing exit; after the header('Location:...'); So it is still sending the headers for the current document when hes trying to do a 302 redirect.

Check that your initial <?php start at the very beginning of the file (UTF BOMs can be nasty on this aspect, but this is not the case); and make sure there is no ?> until you are done with the headers.

After that, look at each function you call: one of them is producing some output. Actually, it must be the one that appears at line 26 on your file. If you don't expect it to produce output, then check the documentation of that function to see why it's producing it. Or update your question with the contents of that line 26 so we can better aid you.

PS: Also check the full output (view source) preceeding that warning. Knowing what is being output may help finding out why it's being output ;)

From the PHP Manual:

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.

So if you want to redirect, it has to be the first thing your script, or the HTML outputs.

Expanding on my comment above:

Considering the position of your commented-out <meta> tag, I'm willing to bet you've already output some HTML before your script hits the header() call.

You can't have ANY data sent to the browser prior to a header() call.

You can add

ob_start();

at the top of your document and it will go away

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM