0

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();
} 


}

1 Answers1

0

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
Jan
  • 2,853
  • 2
  • 21
  • 26
  • If you mean with enclosing the header() function with ob_..., I have tried that, and it doesn't work – user2980782 May 23 '19 at 00:22
  • You have to enclose **everything** before and the header-calls, so nothing gets sent to the browser/client before the headers are set. – Jan May 23 '19 at 00:24