tags:

views:

521

answers:

4

I got a PHP login page, where the user fills in username and password, if it's correct, the page loads some information such as user_id to a session variable. After loading it to the session it's making a header('Location') redirect. (The redirection is to the same domain.) (All pages have session_start(); )

Somehow the next page doesn't recognize the session... how come ??

And I found it more likely to happen in IE than in FF ... strange. I read all stuff on stackoverflow but I follow all guidelines.

Thanks for any help

+3  A: 

Is it possible that cookies aren't enabled?

In order to be able to associate session variables with a specific client instance (ie. how session variables can be used on your browser and my browser at the same time without getting into a conflict), a "session ID" (or "SID") is generated per session. This ID is stored on the server, as well as on the client, usually in the form of a cookie. However, if cookies are not enabled, the session ID is passed along as part of the query string of the URL in each request so that the server can know what session ID belongs to the client.

When you redirect by a header() call, PHP does not automatically insert the SID into the new request, so you will need to append it yourself, in the form of:

header("Location: my_url.com/my_page.php?" . SID)

where SID is a constant defined by PHP that contains the necessary part of the query string (equivalent to session_name() . '=' . session_id(), if a session ID exists).

See Passing the Session ID for more details.

Daniel Vandersluis
Thank you for the fast answer. Can you explain me a bit? what did meen by "propagating the SID"? If this put any more light - just found out that in the localhost it works fine, but on the server something goes wrong.
OfficeJet
I have updated my answer to hopefully be more clear.
Daniel Vandersluis
you are very clear. Thank you for your help. Unfortunately it didn't help and my cookies are enabled, and as I said, on the localhost it works great.Still I can't figure what causing it to dump the session. Is there another way of making redirect in PHP without having the trouble of loosing session variables ?Thank you!
OfficeJet
+1  A: 

Two thoughts:

  1. Is session_start() located at the top of the scripts, before anything is sent to the browser?
  2. Is the domain exactly the same? www.mydomain.com re-directing to mydomain.com would lead to the problem you describe.
jeroen
A: 

Hello,

I just had a similar issue, the solution was to simply add an exit(); instruction under the header(..) redirection.

Hope it helps,

Matt

Matthieu
A: 

header("Location: my_url.com/my_page.php?" . SID) exit();

IT WORKED ONLY AFTER I HAVE INSERTED THE exit() below the header();

irfan