views:

142

answers:

3

On my registration script i have:

// Save registration information into the database

// Set sessions
$_SESSION['var1'] = 'somevalue';
$_SESSION['var2'] = 'anothervalue';

header('Location: /somewhere');
exit();

Then on my login script i have:

// Check if user provided correct login credentials
if (correct) {
    $_SESSION['var1'] = 'somevalue';
    $_SESSION['var2'] = 'anothervalue';
}

header('Location: /somewhere');
exit();

What happened is that the session variables were lost after the header redirect in the registration script but they were preserved after the redirect in the login script.

I've checked session_id() at both pages and they have the same value, included session_start() at the top of every page and basically tried the solutions to this common problem found on Stackoverflow but somehow nothing seemed to work.

I'm beginning to wonder if it is something to do with my server configuration instead of my code.

A: 

Are you redirecting between www.example.com and example.com? Since those are two different domains.

Ólafur Waage
No its redirecting to the same domain. Can't figure out what's wrong :(
Iuhiz
A: 

why use exit()? header("location: /somewhere"); will prevent the remainder of the script to be executed.

Jan Kuboschek
Only if the header is successful. If it fails (eg. output already started) the script will issue a warning and continue. The exit is a good safety net.
Brenton Alker
Hmmm, I didn't know that. Thanks!
Jan Kuboschek
A: 

The manual page about session_write_close has comments from lots of people with this or similar problems. Some say it's fixed by calling session_write_close before the "header('location" line.

This post: http://us.php.net/manual/en/function.session-write-close.php#86791

He says that didn't work for him, but calling session_regenerate_id() did work. (This changes the session cookie, and forces php to send the cookie out.

Sending the cookie again may be required. I've read people saying that some browsers don't send cookies after a redirect, except cookies they just got at the original url.

JasonWoof
Just tried adding session_generate_id() but damn, still not working.
Iuhiz
did you try session_write_close()?
JasonWoof