duanhai4046 2014-10-19 22:14 采纳率: 100%
浏览 24

已经发送了奇怪的PHP标头

I have a simple template rendering system that includes a header, template and footer PHP file. The header has a single PHP function call to render a navigation bar. The template has a function call that sets a header('location ...'); and the footer is just basic 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)

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

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:

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. having the page run as expected is to replace this line in my getNavigation() call:

$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.

  • 写回答

1条回答 默认 最新

  • dousha7645 2014-10-20 01:53
    关注

    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. 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.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值