views:

658

answers:

6

I've got a script that sets some session values before redirecting to / using header().

I've read many posts about the $_SESSION variable being destroyed / lost after header(), even after I implemented this:

// set session here

session_regenerate_id(true);
session_write_close();
header("Location: /");

session_start() is set in the correct places, does anyone know of anything that I might be missing?

On the index.php page I have this:

session_start();
print_r($_SESSION);

// outputs nothing :'(

The code is pretty complex so will not post it all, just snippets.

+2  A: 

I've never seen any session related issues due to using location headers - are you sure you're calling session_start on both pages?


Hmm... this answer made a lot more sense before you added the session_start bits above, and mentioned the fact that you were sure you were using session_start. :-)

middaparka
Even after removing the `session_regenerate_id` line the same problem persists.
ILMV
And yes, I'm sure `session_start()` is being called in all the correct places.
ILMV
If you take out the header location re-direct and simply change the URL (in the browser) is the session data available?
middaparka
Hmmmm... it still isn't set, perhaps it's not a `header()` problem then?
ILMV
I'd recommend using session_start as close to the start of any PHP includes as possible, and make the include the first item (i.e.: pre doctype/HTML headers, etc.) on all pages.
middaparka
Straw clutching thought - I presume the disk area PHP is using for session data isn't full?
middaparka
I've set `start_session();` at the top of each required page...
ILMV
The server is reporting it has loads of space spare... shame I can't get SSH access to the box :-(
ILMV
When I upload the same build to my web server the session works perfectly aft6er the header.
ILMV
Sounds like a fun one. Incidentally, do *all* the partitions on the server have sufficient disk space? (Also, are quotas in use, etc.) Hope you manage to get it sorted anyhow. :-)
middaparka
I only have access to the one partition... shared hosting blows. Any way thanks for your help... +1
ILMV
+1  A: 

header must be send bevore session close

session_regenerate_id(true);

header("Location: /");
// the header must be send bevore session close
session_write_close(); // here you could also use exit();
streetparade
Tried this... no joy.
ILMV
A: 

In the interest of closing this question, we had concluded it was a problem with the server configuration, not surprising considering the host is well known for this kind of thing.

ILMV
A: 

I have a similar problem:

file1.php

....

    setcookie("VCMail",$pep,time()+3600);
    header("Location: file2.php");

file2.php

       if (isset($_COOKIE["VCMail"])){
            $err = $_COOKIE["VCMail"];
       }

       else{
        $err = "NO COOKIE ";
       }
       echo $err;

And I get "NO COOKIE"

dc
A: 

just put exit; after header :D I solved by this

junaid
Assume I've already done thtat
ILMV
A: 

ILMV, what was the server problem and how do u manage to fix the problem, i'm thinking that maybe i have the same problem.

LeoMuller
This shouldn't have been posted as an answer if it isn't one. In the end I had to assume it was a server problem.
ILMV