$_SESSION not maintained

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
apc
Forum Newbie
Posts: 3
Joined: Thu Jun 29, 2006 5:35 am

$_SESSION not maintained

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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. :)
apc
Forum Newbie
Posts: 3
Joined: Thu Jun 29, 2006 5:35 am

Post 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!!!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply