Php &引用;“已发送邮件头”;一个文件中的输出错误,而不是另一个文件
我知道这个问题以前已经被贴过很多次了,但是我一直找不到一个好的答案,因为这个错误并没有像它应该表现的那样 你看,我有类似的代码:Php &引用;“已发送邮件头”;一个文件中的输出错误,而不是另一个文件,php,html,header,Php,Html,Header,我知道这个问题以前已经被贴过很多次了,但是我一直找不到一个好的答案,因为这个错误并没有像它应该表现的那样 你看,我有类似的代码: <?php include("banner.php"); include("menu.php"); print "<div class=\"wrapper\">"; if(true) header("Location:index.php"); else print "Hello World";
<?php
include("banner.php");
include("menu.php");
print "<div class=\"wrapper\">";
if(true) header("Location:index.php");
else print "Hello World";
print "</div>";
include("footer.php");
?>
而menu.php
如下所示:
<div id=banner><a href=index.php><img src=img/banner.png></a></div>
<ul class="menu">
<li class="dropdown">Menu
<ul>
<a href="test1.php?id=<?php print $id; ?>"><li>Item 1</li></a>
<a href="test2.php?"><li>Item </li></a>
<a href="test3.php"><li>Item 3</li></a>
<a href="test4.php?id=<?php print $id; ?>"><li>Item 4</li></a>
</ul>
</li>
</ul>
菜单
请注意,menu.PHP
中有一些PHP元素
如果我运行第一段代码,我会在第5行的/var/www/test.php中得到错误警告:无法修改标题信息-标题已经由(输出从/var/www/menu.php:10开始)发送。php
菜单中的第10行是最后一行。如果我从代码中完全删除菜单,并保留banner.php
,代码就可以正常工作。我发现这令人困惑,因为banner.php
也提供了输出,print''包含菜单后的代码>行
我的问题很简单:为什么menu.php
会触发错误,而banner.php
不会触发错误?任何修改页眉的php都必须在任何其他页面数据之前,除非您打开了输出缓冲。当您输出任何内容时,都会立即发送标头。HTML代码、PHP等
将PHP代码视为一个方面,web服务器就是一个玻璃。让我们想象一下,我们将用HTML的输出填满这个杯子。一旦它得到第一次删除,就已经为它要发送的内容建立了头文件
您可以在PHP.ini中打开输出缓冲
输出缓冲基本上就像一个量杯,让PHP代码填满量杯,当它达到某一点时,就会倒入杯子(Web服务器)。虽然它正在构建中,但它仍然在PHP中,我们可以访问它,这就是为什么我们能够修改我们没有给web服务器任何信息的标题。但是,请注意,这会对服务器的性能产生影响。不过,您可能不会注意到。如果输出被缓冲,并且菜单的最后一行。php
填充缓冲区并将其刷新到线路上,则会发生这种情况。
<?php
include("banner.php");
//The above line the banner output is sent.So does the HTTP Headers
include("menu.php");
print "<div class=\"wrapper\">";
if(true) header("Location:index.php");
//Because the Output is started in bannrd you cannot send additional headers.
else print "Hello World";
print "</div>";
include("footer.php");
?>
如果在php.ini中打开了输出缓冲,则可能会出现不可预测的结果,因为标题
永远不会被缓冲,而内容只会被缓冲。在您的情况下,看起来您在php刷新横幅代码之前发送了标题。可能与重复,仅供参考,这就是罪魁祸首打印”
@Mr.Alien:他在问为什么banner.php
不会触发错误。@SLaks他的标题有误导性:)在那里,我重新表述了问题并更新了标题。你的菜单是否随时调用php函数location
?这似乎是合理的,在我的php.ini
中,我有输出缓冲=4096
,因此,这似乎是有可能的。非常感谢。谢谢你@Alien先生给我小费!