Page 1 of 1

$_SESSION not maintained

Posted: Thu Jun 29, 2006 5:41 am
by apc
Hey everyone.

I recently started developing in PHP and I'm pretty n00b.

Anyhow, I finally made a code that validates a user login and it worked nicely on the IIS installed on my PC.
I'm using $_SESSION to pass parameters between pages. Like I said it works perfectly on my PC.

After I've uploaded the code to my website (http://www.purelan.net, yes I know its a n00b site). The session isn't maintained after I leave the page where I logged in. It looks like $_SESSION isn't passed. Tracking the tmp dir where the session files are created I see that every time I move to another page , a new session file is created.

My big guess is that session_start() is making those files instead of resuming with the session that is succesfuly started, only I don't know why, and more over I've no idea why it works perfectly on my PC and not on my website.


I tried to search the forums for a topic like this and found some nice info, but not a soloution.

Please help PHP masters :)

Posted: Thu Jun 29, 2006 5:56 am
by Jenk
session_start() only creates a new session, if there isn't one already in use.

You need to use session_start() before any output is generated, on every page that you wish the session to be active on, regardless if the session has already started on a previous page, or not :)

eg. Page1:

Code: Select all

<?php
session_start();

$_SESSION['myVar'] = 'value';

header('Location: page2.php');

?>
page2.php:

Code: Select all

<?php
session_start();

echo $_SESSION['myVar']; //outputs 'value'

?>
:)

Other problems can occur when it comes to the location in which the server stores the session variables. Sometimes they are in temporary directory that the host has set to clear every now and then, thus your session data will be lost.

You can configure the sessions to use a different location, have a gander at sessions on php.net for the various directives. :)

Posted: Thu Jun 29, 2006 6:09 am
by apc
Hey Jenk.

I could kiss you right now.


The only way it worked is like you said when I put session_start() at the very top of the page.
Putting it a little bit lower made the entire thing not work.

Thanks a lot!!!

Posted: Thu Jun 29, 2006 10:24 am
by Christopher
apc wrote:The only way it worked is like you said when I put session_start() at the very top of the page.
Putting it a little bit lower made the entire thing not work.
Just remember -- session_start() is what loads the session data into the $_SESSION array.