skip to Main Content

I might need a little advice on my assignment. I’m working on a php script that processes html form and saves the data. The issue I’m having is with the redirection. Basically, if all the data are valid they are saved and then the script should self redirect but it gives me an error saying "Redirect was expected but no ‘Location’ header was found".

This is the relevant part of my code. When I try this locally it works and redirects me but when I submit this to the school submit webpage it gives me the above mentioned error. I used the ob_start() and ob_end_flush() at the beginning and end of the script to make sure there’s no output being sent even tho there shouldn’t be any.

if (count($invalidFields) > 0) {
            recodex_survey_error('Validation failed', array_unique($invalidFields));
    } else {
        // if data is valid save 
        $firstName = $data['firstName'];
        $lastName = $data['lastName'];
        $email = $data['email'];
        // other data here ... 
        
        recodex_save_survey($firstName, $lastName, $email, $deliveryBoy, $unboxDay, $fromTime, $toTime, $gifts, $giftCustom);
        
        header("Location: index.php", true, 302);
        exit;
    }

I would truly appreciate any help or hint of what could I be doing wrong!

2

Answers


  1. The issue you’re facing might be due to the fact that the header() function in PHP must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

    Here’s a step-by-step plan to debug and fix this issue:

    1. Check if there’s any output before the header() function call. This includes any echo, print, or any HTML outside PHP tags. Even a white space before <?php can cause this issue.

    2. Make sure there are no error messages being output before the header() function. If there are any, you should fix these errors.

    3. Use output buffering functions ob_start() at the beginning of your PHP script and ob_end_flush() at the end. This will ensure that no output is sent before headers.

    4. If you’re including or requiring other PHP files, make sure they don’t output anything either.

    5. If none of the above works, you can use ob_get_contents() to check if there’s any output before the header() function. If ob_get_contents() returns anything other than an empty string, then there’s some output being sent.

    Here’s how you can use ob_get_contents():

    ob_start();
    // Your code here...
    if (ob_get_contents()) {
        echo "There's some output before header()";
    }
    header("Location: index.php", true, 302);
    ob_end_flush();
    

    This will help you identify if there’s any output before the header() function. If there is, you should find out where it’s coming from and fix it.

    Login or Signup to reply.
  2. This is a bit a shot in the dark, but what I smell here is that the current header code is of a relative pathname only, also only the basename:

            header("Location: index.php", true, 302);
            exit;
    

    While this can be fine in modern day browsers of the latest generations, doing it this way poses the requirement for all processors during transport to resolve that relative Uniform Resource Locator. Some network components may fail with that and the transport breaks, showing the error message you report.

    Try using the absolute URL for a test and see if it prevents the error:

            header("Location: http://example.org/index.php", true, 302);
            exit;
    

    Can’t promise you this is the solution, and naturally you need to put in the correct URL. But it should be relatively easy to try.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search