header('Location: ...') is not giving immediate redirect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    header('Location: ...') is not giving immediate redirect

    I'm using Win2k with Apache 2, PHP 5.2 for my local test server.

    My problem is that I think the header('Locatio n ...') function isn't sending the header immediately and the rest of my script is running before the page redirects.

    I am using a $_SESSION array to transfer messages from one page to the next. I want the message available only once, then it should be deleted. I had been using the technique something like this:

    [CODE=PHP]
    if (isset($_POST['form_submitted '])) {
    //handle_form
    $_SESSION['message']=$errorMessages ;
    header('Locatio n: http://www.thissite.co m/new_page.php');
    }
    if (isset($_SESSIO N['message'])) {
    $showMessage = $_SESSION['message'];
    unset($_SESSION['message']);
    }
    echo $showMessage;
    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">< input type="hidden" name="form_subm itted" value="v" /></form>
    [/CODE]

    //What I want:
    The form is submitted, the page reloads to itself. The form checks everything and records any errors to $errorMessages. It transfers these to the $_SESSION array and it reloads to a new page.

    //What seems to happen:
    As before... but after the form check, it seems to run the rest of the script, and as the $_SESSION['message'] variable is now set, it goes into the part which unset()s that variable. THEN after this it redirects. Now of course the $_SESSION['message'] is unset and I get no message!

    Does this make any sense? It's a bit convoluted. This is a highly simplified version of my page just to illustrate the process. Any help is very much appreciated!

    Thanks in advance!!

    Henry
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, henryrhenryr.

    Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

    You will still need to use [/CODE] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

    [CODE=html]
    [CODE=javascript]
    [CODE=php]

    and so on.

    Thanks!

    MODERATOR

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, henryrhenryr.

      Call exit() after you set the redirect header:

      [code=php]
      header( ... );
      exit();
      [/code]

      Comment

      • henryrhenryr
        New Member
        • Jun 2007
        • 103

        #4
        Hello pbmods!

        I can't believe I didn't think of that! Genious. Thanks so much!

        Henry

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Henry.

          Originally posted by henryrhenryr
          Hello pbmods!

          I can't believe I didn't think of that! Genious. Thanks so much!

          Henry
          Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

          Comment

          Working...