Never seen this Error...

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
OoZI
Posts: 109
Joined: Mon Jan 02, 2012 4:22 pm

Never seen this Error...

Post 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?
-OoZI

My Blog
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Never seen this Error...

Post 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.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Never seen this Error...

Post 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.
Post Reply

Return to “Advanced Help and Support”