即使使用ob_start()已经发送了标头

时间:2016-05-03 20:54:13

标签: php smarty

我有一个中央的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(),我仍然会收到标题已发送错误。为什么?注意:我正在使用SMARTY模板化工具来创建一些模板,这些模板将通过中心的$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()问题,该站点将在登录/注销时中断。如果我不使用此代码,一切正常。如果将$smarty->display()ob_start()ob_end_flush()结合使用,为什么会出现这种情况?我的意思是:我在整个封闭的$smarty->display()ob_start()内部的特定网站中使用ob_end_flush()很多,我不会打破......

编辑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");

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

1 个答案:

答案 0 :(得分:1)

对于其他有同样问题的人:不,我不是我的错,这是一个聪明的错误。更新已修复,以下是说明:https://github.com/smarty-php/smarty/issues/187

相关问题