I have seen references to this problems and i remember discussing it w hile back but I cannot find a valid answer..

If header(Location....)
is called after some data has been set with $SESSION['data'] = .....
The session data will not be set.
If the redirect to page is called manually, it works fine. Passing the session id in the url has not effect. Watching the session file, It seems that the $
SESSION array is not written to the file until the page expires even though teh array data is availabl within the page. If the header("location is called, nothing gets written to teh session files so this has nothing to do with passing the session id along.

I vaguely remember there was a solution to this but I cannot find it. Any ideas?
Joshua

    Heh. anwer my own post 5 minutes after I ask 🙂 jogged my memory.

    For thos with the same problem.

    You MUST call session_write_close() before any header redirect calls for the session data to be set.

      a month later

      Hello Everyone,

      This session_write_close(); function does not fix the problem with me.
      I have put it just in front of header("Location: index.php");

      The page redirects but when I try browsing the site using my menu on the index page, the link contains a PHPSESSID= but it seems to be the wrong one so I have to log in again and then it does not work again because of this redirect .

      However, when I have a normal link instead of the redirect it works fine.

      Anyone?

      Regards,

        Hmm,
        Is php compiled with trans_sid and is it set in your ini file?

        I sometimes get this PHPSESSID thing but my session id has a different name and always works, so I'm not sure where that one comes from.
        You could also try forcingt the session id to be passed as part of the redirect call. I forget the syntax though. Something like index.php".SID or something (see manual)
        Also some browsers have issues with relative url's. You may want to pass the whole [url]http://...thing.[/url] Probably wont help with your problem though

          Hello Happycloud and everyone,

          1. trans_sid is set to 1
          2. I tried changing the PHPSESSID but it still has the same problem
          3. tried forcing the PHPSESSID but it does not work
          4. tried [url]http://[/url] etc. but it does not work

          this is in fact what is happenning

          -i go to index page and a session is started so the session cookie is created,

          • i then pass the login details onto a login page using a form in index page so the cookie is updated with a variable in it.

          -so we are still at the same session (cookie)

          -now, if i have a link on the login page so that I can go back to the index page everything works fine because no new cookies are created in my c:\php\tmp dir.

          -however if instead of a link I have a redirection header what it does once it gets to index is that it creates a new cookie in c:\php\tmp which is empty

          -then if I try browsing the menu which is on my index page the actual PHPSESSID=44997ae0636c7a33ec008d79df3eb6f4 contains this number which is the number of the new empty cookie which has just been created after redirection as explained in the point above.

          -I then have to log in and we have a viceous circle, however it instead of redirect i have a link it all works

          ANOTHER QUESTION: is there a function which could stop wirtting of any more new cookies in c:\php\temp

          Regards

            This works for me:

            //index.php
            <?php
            session_start();
            if(isset($SESSION['mydata'])) {

            echo "Session data is: " .$
            SESSION['mydata'];
            }
            ?>
            <br/>
            <form action="login.php" method="post">
            <input type="text" name="mydata" size="20"/>
            </form>

            //login.php
            <?php
            session_start();
            if(isset($POST['mydata']))
            {
            echo "Setting session data....";
            $
            SESSION['mydata'] = $POST['mydata'];
            }
            session_write_close();
            echo $
            SESSION['mydata'];
            header("Location: index.php");
            ?>

              Hello again happycloud,

              Right. We are finally getting somewhere. I can see that your code shown in the example above should work. What should happen is that if the data is set then it should echo data above the field however if it is not nothing should be displayed. You then enter data and it should then redirect you back with the data displayed above the text field.

              However, that does not happen with me. I believe you that it works on your computer it is just that it does not on mine. I think it must be something with settings because each time the index page is redirected to a new cookie is written in c:\php\tmp which is empty. If I then enter a value in the text field what it does is it updates the empty cookie with the data from the text field and as soon as it is redirected back to index it creates a new one, which is empty.

                I'm out of ideas here...
                I can send you my php.ini file and my phpinfo( dump if you think it would be helpful. You're on windows so it's gonna be different.
                All I can think of is:
                phpinfo() and make sure trans_sid is compiled AND set
                and globals is off (at least for my script to work)
                Not sure what else...

                  18 days later

                  Hello Loudcloud and everyone,

                  I found the anser to our problem a while back and this is it:

                  ; The path for which the cookie is valid.
                  ;session.cookie_path = C:\PHP\tmp
                  session.cookie_path = /

                  in php. ini the path used to be: C:\PHP\tmp and I just made it / and it now works, A low lever trace had to be performed.

                    Write a Reply...