PHP - Cannot modify header information...

Posted by Scott W. on Stack Overflow See other posts from Stack Overflow or by Scott W.
Published on 2010-12-24T06:47:42Z Indexed on 2010/12/24 6:54 UTC
Read the original article Hit count: 275

Filed under:
|

Hi, I am going crazy with this error: Cannot modify header information - headers already sent by...

Please note that I know about the gazillion results on google and on stack overflow. My problem is the way I've constructed my pages. To keep html separate from php, I use include files. So, for example, my pages look something like this:

<?php 
require_once('web.config.php'); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link rel="shortcut icon" href="images/favicon.gif"/>
<link rel="shortcut icon" href="images/favicon.ico"/>
<link rel="stylesheet" type="text/css" href="<?php echo SITE_STYLE; ?>"/>
</head>
<body>
<div id="page_effect" style="display:none;">
<?php require_once('./controls/login/login.control.php'); ?>
</div>
</body>
</html>

So, by the time my php file is included, the header is already sent.

Part of the include file looks like this:

// redirect to destination
if($user_redirect != 'default')
{
    $destination_url = $row['DestinationUrl'];
    header('Location:'.$user_redirect);
}
elseif($user_redirect == 'default' && isset($_GET['ReturnURL']))
{
    $destination_url = $_GET['ReturnURL'];
    header('Location:'.$destination_url);
}
else
{
    header('Location:'.SITE_URL.'login.php');
}

But I can't figure out how to work around this. I can't have the header redirect before the output so having output buffering on is the only thing I can do. Naturally it works fine that way - but having to rely on that just stinks.

It would be nice if PHP had an alternative way to redirect or had additional parameters to tell it to clear the buffer.

© Stack Overflow or respective owner

Related posts about php

Related posts about header