简体   繁体   English

已发送奇怪的PHP标头

[英]Strange PHP Headers already sent

I have a simple template rendering system that includes a header, template and footer PHP file. 我有一个简单的模板渲染系统,其中包括页眉,模板和页脚PHP文件。 The header has a single PHP function call to render a navigation bar. 标头具有单个PHP函数调用,以呈现导航栏。 The template has a function call that sets a header('location ...'); 该模板有一个函数调用,用于设置标头(“ location ...”); and the footer is just basic HTML. 页脚只是基本的HTML。

When I view the page, I get a 'headers already sent' error, specifying the line in the header that makes the call to echo out the navigation bar. 当我查看页面时,出现“已发送标题”错误,在标题中指定进行呼叫以呼出导航栏的行。 If I modify the code to remove the child elements in the navigation, I don't get the 'headers already sent' error. 如果我修改代码以删除导航中的子元素,则不会收到“已发送标题”错误。 The child elements are nothing more than array items. 子元素不过是数组项。 If I replace them with dummy text, then the page redirects correctly. 如果我将其替换为伪文本,则页面将正确重定向。

I imagine it's something very simple, but it has me stumped! 我想这很简单,但是却让我感到困惑!

header.php (the int passed in specifies the depth to scan) header.php(传入的int指定要扫描的深度)

<ul class="nav navbar-nav"><?php echo getNavigation(1); ?></ul>

template.php 的template.php

// Run from start
default:
        $_SESSION['updateStart'] = microtime(true);
        $_SESSION['updateErrors'] = array();
        $_SESSION['updateLog'] = array();
        header('location: /update-products?action=getDataFeeds');
        exit();
break;

getNavigation function: getNavigation函数:

function getNavigation($depth) {
    global $path, $misc;

    $items = $misc->getNavigation(1,(int)$depth,true);
    $nav = "";

    if($items) {
        foreach($items as $item) {
            $class = ($item['ob_alias']==("/".$path[0])?' class="active"':'');
            $nav .= '<li'.$class.'>';

            if(isset($item['children'])) {
                $nav .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$item['ob_label'].' <span class="caret"></span></a>';
                $nav .= '<ul class="dropdown-menu" role="menu">';
                foreach($item['children'] as $child) {
                    $class = ((isset($path[1]) && $child['ob_alias']==("/".$path[1]))?' class="active"':'');
                    $nav .= '<li'.$class.'><a href="'.$child['ob_alias'].'">'.$child['ob_label'].'</a></li>';
                }
                $nav .= '</ul>';
            } else {
                $nav .= '<a href="'.$item['ob_alias'].'">'.$item['ob_label'].'</a>';
            }
            $nav .= '</li>';
        }
    }       
    return $nav;
}

I've tried to walk through my code and all that I can seem to do to toggle the error Vs. 我试图遍历我的代码以及似乎可以做的所有事情来切换错误Vs。 having the page run as expected is to replace this line in my getNavigation() call: 使页面按预期运行将替换我的getNavigation()调用中的这一行:

$nav .= '<li'.$class.'><a href="'.$child['ob_alias'].'">'.$child['ob_label'].'</a></li>';

with

$nav .= '<li>Some dummy text</li>';

Then it's fine. 那很好 I can't see why including the two array item strings would break the 'headers'. 我看不到为什么包含两个数组项字符串会破坏“标题”。

Any help, or pointers for things to test would be most welcome. 任何帮助,或需要测试的东西的指针都是最欢迎的。

Seeing the order you are loading all of this may help but based on what you have, I would suspect you are rendering something (like the nav) before setting the headers in template.php. 查看您正在加载的所有内容的顺序可能会有所帮助,但是基于您所拥有的内容,我怀疑您正在渲染某些内容(如导航),然后在template.php中设置标题。 You may want to reference this answer as well as post more specific code that show in what order the template files are being loaded. 您可能要参考此答案 ,并发布更具体的代码,这些代码以什么顺序显示模板文件的加载。

Functions that send/modify HTTP headers must be invoked before any output is made. 

Otherwise the call fails.

Output can be:

Unintentional:

   Whitespace before <?php or after ?>
   UTF-8 Byte Order Mark
   Previous error messages or notices

Intentional:

   print, echo and other functions producing output (like var_dump)
   Raw <html> areas before <?php code.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM