I am getting the Warning: Cannot modify header information. I am NOT getting this error when testing the source code on WAMP. Also in some of the PHP certain classes are included and functions to manipulate the html are called as needed. Can any one point me in the right direction? Here’s the exact error and the cited files.
Warning: Cannot modify header information – headers already sent by
(output started at
/home/content/39/8810539/html/pagoda/view/header.php:2) in
/home/content/39/8810539/html/pagoda/controller/login.php on line 9
login.php:
<?php
global $session;
global $view;
if (isset($_POST['login']))
{
if ($session->logIn($_POST["uname"],$_POST["password"]))
header("Location: $_SERVER[REQUEST_URI]");
else
$view->RenderMsg("Your username and/or password was incorrect.");
}
if (isset($_POST['logout']))
{
$session->logOut();
header("Location: $_SERVER[REQUEST_URI]");
}
?>
header.php snippet:
<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASEPATH; ?>/view/css/homepageStyle.css" />
</head>
<div id="header">
<div id="banner">
Pagoda
</div>
<div id="login">
<?php
//This controls the login/logout header on the top of each page.
global $session;
if ($session->isLoggedIn())
{
?>
<div id="nametag">
<form method="POST" name="logout" action="">
Welcome, <?php echo $session->getName()." (".$session->getRole().")"?>
<input name="logout" type="hidden" value="Log Out"/>
</form>
</div>
....
....
....
I solved it with ob_start(). ob_start writes the html to a buffer first, then it writes it to the browser after the header has changed. ob_start goes before your html code. It’s not necessary but good practice to close the buffer after the header() with ob_end_flush();
header.php snippet:
login.php