headers 即使使用 ob_start() 也已发送

headers already sent even if using ob_start()

我有一个中央 index.php 打印基本 html 模板,如:

<?php ob_start(); ?>
<html>
<head>
</html>
<body>

{dynamic php here (might include $smarty->display()}

</body>
</html>
<?php ob_end_flush(); ?>

每当需要特定站点时,我都会将其包含在中心并在那里执行。以用户身份登录时,在完成所有验证检查后,我需要通过 header() 重定向。但我不能,因为一些 HTML 已经打印出来了。好的,没问题。我在开始时用 ob_start()ob_end_flush() 包围了我的 index.php;在它的末尾。即使我使用 ob_start()ob_end_flush(),我仍然收到 headers already sent 错误。为什么?注意:我正在使用 SMARTY templating enginge 来制作一些模板,这些模板将通过中心的 $smarty->display() 显示。

这里有人知道为什么会这样吗?

谢谢!

编辑:实际代码:

<?php
session_start();
ob_start();
/**
 * Main index.php, root template
 */

?>

    <!DOCTYPE HTML>

    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 0);
    ini_set("log_errors", 1);
    ini_set("error_log", "logs/ERROR_LOG.txt");

编辑 2:

已将问题缩小到 nav.php 中的 2 个更改,其中包含站点的导航。导航是一堆:

if($user->hasPermissionFor("specialsite")) {
    ?>

    <li><a href="...">MyLink</a></li>

    <?php
}

我简单地添加了(自上次提交和实际代码以来):

$smarty->display("GENERAL_LANGUAGE_PICKER.tpl");

GENERAL_LANGUAGE_PICKER.tpl 是一个简单的 HTML 模板,显示一些语言 png 以选择一种语言。没什么特别的。如果我使用此代码,由于 header() 问题,该站点将在 login/logout 处中断。如果我不使用此代码,一切正常。如果不应该将 $smarty->display()ob_start()ob_end_flush() 结合使用,为什么会中断?我的意思是:我在整个包含 ob_start()ob_end_flush() 的特定站点中使用了很多 $smarty->display(),我不会破坏...

编辑 3: 我只是无法让它工作。这又是简单地从项目中复制出来的代码:

<body>

<nav class="navbar navbar-inverse navbar-fixed-top" id="tourstep-two">
    <?php
    // Generate nav for this file, here i call nav.php in which i have 2x 
    // $smarty->display() which will break the code
    include_once("php/intern/nav.php");
    ?>
</nav>

<header style="background: url(images/layout/banner.png) no-repeat" class="banner">
    <img class="img-responsive img-center"
         src="images/layout/logo.png"
         alt="Logo">
</header>

<section id="tourstep-six">
    <div class="container">
        <div class="col-lg-12">

        <?php
        // Here i am in the main content area. When using this $smarty->display everything is FINE and it does work
        $smarty->display("templates/GENERAL_SUCCESS.tpl");

好的,所以有 2 个部分我想调用 $smarty->display()。在呈现导航的 nav.php 内部,一次出现在主要内容区域的开头。在主要内容区域这样做时,它工作正常,我没有收到任何错误。一旦我在 nav.php 中尝试这个,它就会中断并且 HTML 不会写入缓冲区,而是写出并中断我的 header() 调用。我不知道为什么。如果我将 nav.php 中的 $smarty->display() 调用替换为它们正在渲染的 HTML,它就可以工作。然而,通过调用它不会被写入缓冲区。聪明的到底在做什么,为什么它不让我把它写在 nav.php 里面?任何帮助表示赞赏...

对于遇到同样问题的其他人:不,我不是我的错,这是一个聪明的错误。更新修复了它,这里是描述:https://github.com/smarty-php/smarty/issues/187