SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict tbakerisageek's Avatar
    Join Date
    Sep 2006
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setting $_SESSION['variable'] before header redirect not working

    I'm implementing a "Status Message" system on a site I'm developing. The idea is that when the contents of a form get processed the script processing the form can give feedback to the user.

    I've created a few functions to aide me in this. They are as follows.
    PHP Code:
    <?php
        
    /********************************************
        *
        *    Message Functions to handle processing 
        *    Messages from one page to another
        *
        *
        ********************************************/
        
        
    function message_set($messageText$messageType=''){
            
    $_SESSION['message']['text'] = $messageText;
            
    $_SESSION['message']['messageType'] = 'message'.$messageType
        }
        
        function 
    message_get(){
            echo 
    '<div id="'.$_SESSION['message']['messageType'].'">'.$_SESSION['message']['text'].'</div>';
            unset(
    $_SESSION['message']);
        }
        
            
    ?>
    This works nicely in most cases. I can't seem to get the $_SESSION['message'] to "Stick" if it is used just before a header redirect...

    My forms submit to themselves. above any "outputting" code, it checks to see if the form was submitted and handles the data from there. Sometimes it continues the page and loads another form, sometimes it redirects to another page using header('Location: xyz.php');

    Under the following context, I can't get my message_set() function to work
    PHP Code:
        message_set('User Account Added Successfully');
        
    header('Location: index.php'); 
    on the index.php page, I've print_r($_SESSION);'ed and I see the rest of the Session Variables that I've set so far but the new one I want to use is not there.

    For testing, I've added echo 'Message Set' at the end of the 'Message Set' function to ensure that the function IS running. It is, but the SESSION variable Just does not persist.

    Any Ideas? Thanks in advance.

  2. #2
    SitePoint Addict tbakerisageek's Avatar
    Join Date
    Sep 2006
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've found my answer.

    Aparantly, its a function of how PHP/Apache uses the HTTP Protocol. Because there was no "Sending Data" to the client browser before the redirect, the browser never knew of the page. This also means that the session handling cookie was not updated before the redirect.

    The issue was resolved by using session_write_close(); before the header(); redirect. Doing this, forces the server to update the cookie before the redirect thus, making the session variable to that particular browser instance.

    Technically, the Session variable was being created but the browser was not updated to let PHP know that it should have access to it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •