简体   繁体   中英

Error: Cannot modify header information - headers already sent by

I seem to keep getting this error: Cannot modify header information - headers already sent by...

I don't understand what I am doing wrong. The header file is just a normal header... view code

The weird thing is, it works without any errors on wampserver, but it has this error on a hostingserver.

I tied to check for white spaces and weird outputs, but cannot find any?

<?php 
require 'header.php'; 
require 'includes/dbh.inc.php'; 

// Search system

if (isset($_POST['submit'])){
$aMust = $_POST['must'];
$aOptional = $_POST['optional'];
$category = $_POST['category'];


if ($_POST['category'] == "Breedables"){
    header("Location: categories/breedables.php?must=".$aMust."&optional=".$aOptional);
    exit();
} else if ($_POST['category'] == "Jobs"){
    header("Location: categories/jobs.php?must=".$aMust."&optional=".$aOptional);
    exit();
} 


}

You get this error, because something is already sent back to browser when you are calling the header-function. And setting headers is only possible before sending anything back to the browser, because the headers have to be sent before any content.

If you really cannot find what is sent to your browser before calling header, you could you output-buffers :

<?php

ob_start();

// some php code

header('My custom header');

$ob_content = ob_get_contents();
ob_end_clean();
echo($ob_content);
// Or as an alternative to the three lines above use: ob_end_flush();

// other/more code

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