Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top