我们的网站已经上线几年了,有些事情发生了变化,这真的很奇怪。我尝试与房东交谈,但没有解决任何问题。
问题是第一个 <?php
之前的所有内容第一个包含文件中的标记在进入 OB 之前直接获取输出
例如:
<?php
ob_start();
?>
<h1>Welcome</h1>
<?php
include('contentbox1.php');
include('contentbox2.php');
include('contentbox3.php');
?>
<b>Bye</b>
<?php
$content = ob_get_contents();
ob_end_clean();
echo("Included{".$content."}");
?>
得到的输出是这样的:(注意 <h1>Welcome</h1>
不与缓冲区内输出的其余内容一起)
<h1>Welcome</h1>
Included{
<div id="contentbox1"></div>
<div id="contentbox2"></div>
<div id="contentbox3"></div>
<b>Bye</b>
}
正确的输出应该是什么(并且在我的本地计算机上):
Included{
<h1>Welcome</h1>
<div id="contentbox1"></div>
<div id="contentbox2"></div>
<div id="contentbox3"></div>
<b>Bye</b>
}
这给我们的网站带来了重大问题,但我不明白为什么。它在我的本地计算机上完美运行,只是不是我们多年来使用的托管。
解决方案:
事实证明,当我重新上传test.php
时,我网站上的内容盒和许多其他文件变得很有趣,并且可能已损坏。一旦我重新上传整个网站(即使没有发生代码更改),运行缓慢的网站现在速度非常快,所有问题都消失了。我的文件也被其他主机损坏了,所以如果您将来也遇到奇怪的事情,请记住这一点。
最佳答案
您的代码已正确执行,因为 ob_get_clean 的工作原理如下:
ob_get_clean — Get current buffer contents and delete current output buffer
Returns the contents of the output buffer and end output buffering. If output buffering isn't active then FALSE is returned.
使用ob_get_contents();
ob_start();
include('test.php');
$content = ob_get_contents();
ob_end_clean();
echo("Included{".$content."}");
更新:
<?php
ob_start();
?>
<h1>Welcome</h1>
<?php
include('contentbox1.php');
include('contentbox2.php');
include('contentbox3.php');
?>
<b>Bye</b>
<?php
$content = ob_get_contents();
ob_end_clean();
echo("Included{".$content."}");
?>
关于php - ob_start 并包含问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15147179/