Page 1 of 1

Never seen this Error...

Posted: Tue Jan 01, 2013 11:55 pm
by OoZI
So I am getting this error: Warning: Cannot modify header information - headers already sent by (output started at /www/zzl.org/m/e/r/merchant-marine/htdocs/game/logout.php:24) in /www/zzl.org/m/e/r/merchant-marine/htdocs/game/logout.php on line 32


For this Code:

Code: Select all

<?php

include_once('connect.php');
include_once('auth.php');



$user = $_SESSION['login'];
$rank = $_SESSION['rank'];
$ship = $_SESSION['ship'];
$image = $_SESSION['image'];

?>

<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<center>
<a href="index.php"><img src="../images/banner.jpg" /></a>
</center>
<center>
<a class="menu" href="index.php">Home</a><a class="menu" href="ship.php">Ship</a><a class="menu" href="inbox.php">Inbox</a><a class="menu" href="work.php">Work</a><a class="menu" href="store.php">Store</a><a class="menu" href="bank.php">Bank</a><a class="menu" href="logout.php">Log Out</a></center>
<div class="body">
<br /><br />
<!-- START EDITABLE BODY -->
<?php

session_destroy();
ob_start();
header ('location: http://merchant-marine.zzl.org');
ob_end_flush();
?>
<!-- END EDITABLE BODY -->
</div>
</body>

</html>
How do I fix that and make my redirect work?

Re: Never seen this Error...

Posted: Wed Jan 02, 2013 2:16 am
by Jackolantern
A header redirect must sent before any text (aka PHP code) is sent to the browser. That means conditional and timed redirects with headers are out. I suggest to redirect with Javascript instead, which gives you the option to either make conditional redirects on the client-side, or timered redirects after the page has been displayed.

Re: Never seen this Error...

Posted: Wed Jan 02, 2013 7:17 pm
by Xaleph
What Jack said, however to prevent it, you can also circumvent this by using output buffers. To do so, before you begin your PHP code ( somewhere all the way on the top of the first file you load ) use: ob_start(); and when you are done loading and doing all the logic, and when you finish your html output, use this: ob_flush();

Now, you can use headers inside output, which is not a clean way, but helps to get the annoying error out. Basically, the error is something like, you are driving a car but you open the door while driving, a red flag correct? That`s how PHP also sees it.