PHP - 无法修改标头信息

2022-01-11 00:00:00 header php

这个错误我快疯了:无法修改标头信息 - 标头已由...发送.

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

请注意,我知道谷歌和堆栈溢出的海量结果.我的问题是我构建页面的方式.为了将 html 与 php 分开,我使用包含文件.因此,例如,我的页面如下所示:

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>

所以,当我的 php 文件包含在内时,标头已经发送.

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')
{
    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.

如果 PHP 有另一种重定向方式或有额外的参数告诉它清除缓冲区,那就太好了.

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

推荐答案

header 语句前是否有 echo/print 语句?这可能会导致此错误.

Is there any echo/print statement before the header statements? That could cause this error.

相关文章