对 header() 的调用偶尔会失败,并显示“标头已发送”

发布于 2024-12-29 23:04:31 字数 2380 浏览 1 评论 0原文

我的网站中的文件 findRecords.php 中有稳定的 php 代码,如下所示:

 // inside findRecords.php....
<HTML>  
<head>
<?php 
include 'titleBar.php';
include 'topNavigationBar.php';
?>    
</head> 
require 'varsAndStatics.php'; // variables and statics used throughout

// other html in findRecords.php not shown here for brevity.....

在我的 topNavigationBar.php 中,我输出了我的 top-of-every- 的 html-页面导航栏,大致如下:

<div class="pageTopRowContainerLabel">
    <a class="pageTopRowTextStyle" 
       href="http://localhost/myProj/index.php">HOME</a>
</div>   
  // more nav bar divs not shown....

在 findRecords.php 中,我进行了一个简单的数据库查找并获取了一些记录,并打算通过使用 header() 切换到不同的浏览器来在浏览器中显示这些记录显示的页面这些记录 (showRecords.php)。

如果您注意到上面的内容,您可以在 findRecords.php 中看到,已经通过包含“topNavigationBar.php”的方式发送了一个 html 标头,其中包含一些用于显示我的导航栏的 html div陈述。

然后我在同一个 findRecords.php 文件中调用 header() 来实现 Post/Redirect/Get:

header("Location: http://localhost/myProj/showRecords.php", true, 303);

上述工作正常。 在数据库中查找记录后,我header()showRecords.php 来显示它们。

好吧,现在我刚刚向 varsAndStatics.php 添加了一个定界文档,该文档已包含在上面的 findRecords.php 中(见上文)。我现在将以下定界文档添加到长期稳定的文件 varsAndStatics.php 中:

echo <<<_RIDOFWHITESPACE
<br />
<script type="text/javascript">

function ridOfWhiteSpace(theFormElementFieldValue, bLtrsOnly, bLtrsNumsOnly)
{

   //  Remove whitespace
   if(theFormElementFieldValue.indexOf(" ")!=-1) 
   {
      var doozh = theFormElementFieldValue.split(" ").join("");
      theFormElementFieldValue = doozh;
   }
   return theFormElementFieldValue;
}
</script>
_RIDOFWHITESPACE;

现在,我对 findRecords.php 中上面的 header() 的调用中断了 标头已发送错误。

我不知道为什么heredoc破坏了这个稳定的代码。毕竟——对 header() 的调用和导航条码已经存在了一段时间了!

我的意思是这样的:由 findRecords.php 发送的页面由于包含 topNavigationBar.php 而发送导航栏 div - 然后我调用 header() -- 该代码在相当长一段时间内运行良好。

在我看来,因为已经有输出发送到浏览器(topNavigationBar.php divs) 当我打电话时在旧的稳定代码中的 header() 中,在 varsAndStatics.php 中添加 heredoc() 不应破坏代码。

我在这里缺少什么微妙之处吗?

I've had stable php code in my site in a file findRecords.php as follows:

 // inside findRecords.php....
<HTML>  
<head>
<?php 
include 'titleBar.php';
include 'topNavigationBar.php';
?>    
</head> 
require 'varsAndStatics.php'; // variables and statics used throughout

// other html in findRecords.php not shown here for brevity.....

Inside my topNavigationBar.php I output the html for my top-of-every-page navigation bar, along the lines of:

<div class="pageTopRowContainerLabel">
    <a class="pageTopRowTextStyle" 
       href="http://localhost/myProj/index.php">HOME</a>
</div>   
  // more nav bar divs not shown....

Inside findRecords.php I do a simple database lookup and get some records and intend to display those records in the browser by using header() to switch over to a different page that displays those records (showRecords.php).

If you notice above, you can see above that in findRecords.php, an html header has already been sent with some html divs used to display my navigation bar, by way of the include 'topNavigationBar.php' statement.

Then I have a call to header() inside this same findRecords.php file to implement Post/Redirect/Get:

header("Location: http://localhost/myProj/showRecords.php", true, 303);

The above works fine. Upon finding records in the database I header() over to showRecords.php to display them.

Okay now I just added a heredoc to the varsAndStatics.php that has been included all along above in findRecords.php (see above). I now add the following heredoc to the long-time-stable file varsAndStatics.php:

echo <<<_RIDOFWHITESPACE
<br />
<script type="text/javascript">

function ridOfWhiteSpace(theFormElementFieldValue, bLtrsOnly, bLtrsNumsOnly)
{

   //  Remove whitespace
   if(theFormElementFieldValue.indexOf(" ")!=-1) 
   {
      var doozh = theFormElementFieldValue.split(" ").join("");
      theFormElementFieldValue = doozh;
   }
   return theFormElementFieldValue;
}
</script>
_RIDOFWHITESPACE;

Now my call to header() above in findRecords.php breaks with a Header already sent error.

I do not know why the heredoc broke this stable code. AFTER ALL -- that call to header() and the nav bar code have been there for a while!

What I'm saying is this: the page sent by findRecords.php sends the nav bar divs due to the include topNavigationBar.php -- and then I call header() -- that code has worked fine for quite a while.

In my opinion, because there has been output sent to the browser (the topNavigationBar.php divs) when I call header() in the old stable code, the adding of a heredoc() in varsAndStatics.php should not break the code.

Is there a subtlety I"m missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2025-01-05 23:04:31

看来您在这些测试期间以某种方式更改了 output_buffering 设置。

在 findRecords.php 中,我进行了一个简单的数据库查找并获取了一些记录,并打算通过使用 header() 切换到显示这些记录的不同页面( showRecords.php)。

我认为这没有任何意义。为什么不能将用户引导至 showRecords.php?或者只是包含它,而不进行重定向?

It seems that you have the output_buffering setting changed somehow during these tests.

Inside findRecords.php I do a simple database lookup and get some records and intend to display those records in the browser by using header() to switch over to a different page that displays those records (showRecords.php).

I see no point in this. Why can't you direct user already to showRecords.php? Or just include it, without redirects?

π浅易 2025-01-05 23:04:31

仍然没有看到为什么 header() 已经工作了好几个星期的解释,尽管事实上 findRecord.php 一直在发送带有 html 标题的导航栏 html,并且我对 header() 的调用工作得很好。只是这里一些人的猜测——没有答案。我花了一天的时间检查了页面顶部导航栏的代码以及 html 和 div,然后对 header() 的调用效果很好。然后,一旦我取消对heredoc的注释,当遇到对 header() 的调用(已经存在,工作正常,数周)时,我就会收到“标头已发送”警告。

我的代码中没有任何内容可以解释为什么在过去几周发送导航栏 html 之后对 header() 的调用运行良好。请不要对我的问题添加任何更多的猜测!如果您不知道答案,请不要再把这个问题搞得一团糟。做出 SWAGS(愚蠢的疯狂猜测)“看起来你的输出抖动设置在这些测试期间以某种方式发生了变化”是多层次的猜测,没有帮助。

我会找出为什么我的导航栏 html 输出和 header() 调用工作得很好,以及为什么它们在添加定界符时中断,然后将其发布回此处。

Still have not seen an explanation for why header() has worked for weeks despite the fact that all this time, findRecord.php has been sending my navigation bar html with the html header and my call to header() was working just fine. Just speculation from some folks here -- no answer. I spent the past day reviewing the code and the html and divs for the navigation bar on the top of the page, then the call to header() work great. Then as soon as I un-comment the heredoc I get a 'header already sent' warning when the call to header() (that has been there, working fine, for weeks) is encountered.

There's nothing in my code that I can see to explain why the call to header() has worked fine these past weeks occurring after the nav bar html has been sent. Please do not add any more speculation to my question! If you don't know the answer, do NOT clutter up this question ANY MORE. Making SWAGS (silly wild-aced guesses) like "seems like your output buffetting setting changed somehow during these tests" is mutiple levels of guesses and not helpful.

I will find out why my nav bar html output and header() call are working just fine and why they break when a heredoc is added, then post it back here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文