0

I have added some server side validation to a dropdown box on one of my pages and when I did the page wouldn't work anymore. The code I added is as follows:

$show_form = true;        
if (isset($_POST['submit'])) {
    //All of the server side validations   
      $validator = new FormValidator();
    $validator->addValidation("hospital_name","dontselect=000","No facility was chosen");

    if ($validator->ValidateForm()) {
    // All the variables from the submission form   
    $userid = $_SESSION['user_id'];
    $hosp = $_POST['hospital_name'];
   header('Location: ../site_hospital' . $hosp . '/hospital_submitform.php?usr=' . $userid . '&&hosp=' . $hosp);
    exit;
    $show_form = false;
    } else {
    echo "<B style='color:red;'>The following errors occurred:</B>";
    $error_hash = $validator->GetErrors();
    foreach ($error_hash as $inpname => $inp_err) {
        echo "<p style='color:red;'>$inp_err</p>\n";
    }
}}        
if (true == $show_form) {

Through pure chance I added ob_start(); as part of my debugging to the beginning of the page and suddenly my code worked properly but I have no idea why and I was hoping the community could throw out an educated guess as to why. When the code stopped working it would not execute my header command above, the page would simply refresh and not change location, when I added ob_start(); to the top of the page the page redirected as planned. So the overall question is why would the page not direct using the header command without ob_start? I'm sure alot more detail and code is necessary for a definitive answer but I'm hoping someone has run into this before or has an educated guess that may lead me to my own answers. Thanks for any insight.

1

1 Answer 1

2

it's because you were writing to the output stream and preventing the header from working properly. once you started buffering other outputs, you removed the obstacle to the header's operation.

4
  • Thank you for the quick response. I'm looking over the code now for any offenders and nothing jumps out at me. Could you give me an example of "writing to the output stream" for me and anyone else who reads this post? Is that simply echo statements?
    – Dev Newb
    Mar 11, 2012 at 18:22
  • hard to say, could be whitespace before the opening <?php or could be echo statements. of course, it just occurred to me that one way to find out is to use ob_get_contents and see what's in the buffer.
    – dldnh
    Mar 11, 2012 at 18:27
  • Thanks for the help. I'll see if I can figure it out from here. If I can't do you advise against using ob_start() as a patch for now until I have time to come back and figure out the problem? Any potential pitfalls?
    – Dev Newb
    Mar 11, 2012 at 18:36
  • I think you wouldn't want to leave ob_start in for a long time, especially in production, but certainly until you're able to determine the need for it. you know, I thought of something else. with ob_start in place, if you do an echo with a unique ID every few lines in your code, perhaps that will assist in narrowing down the trouble spot. -- assuming, of course, that you're using ob_get_contents and dumping that to a log so you can see that debug output.
    – dldnh
    Mar 11, 2012 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.