[solved] A session that is... well, not working

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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

[solved] A session that is... well, not working

Post by robster »

Hi all,

I have a set of user registration pages that takes input via a form, passes data to the session and then has a bunch of php pages that do checks on the data in the session.

I have a particularly strange situation hapening, where I write data to the session with this code:

Code: Select all

$_SESSION['registration_details']['stage_2_pass'] = $pass2;
which works fine, as reading it with this code:

Code: Select all

echo "<br>stage_2_pass is ".$_SESSION['registration_details']['stage_2_pass']."<br>";
outputs the following:

Code: Select all

stage_2_pass is 0
(noting that the contents of $pass2 was of course, '0';)

Now what's hapening (the problem) is I have a page that reloads upon a problem to show the user their choices again so they can ammend their mistakes/fill in blanks etc.

The problem is, when I do a:

Code: Select all

if ($_SESSION['registration_details']['stage_2_pass'] == "0")
{
//do stuff
}
on that page, well, it doesn't do it. So upon looking into it further, if I try and do an echo of what's in the session on that same page, it shows nothing. IE: if I were to do this:

Code: Select all

echo "stage_2_pass = ".$_SESSION['registration_details']['stage_2_pass']."<br>";
then I get nada. nothing. zilch :(

It's so strange, seeing as another page (my read the session test page) can perform that same command and actually echo the contents, but on this page, it can't.

Can anybody point me in the correct direction or give me some ideas I can much about with?

I'd really appreciate it.

Thanks so much.

Rob
Last edited by robster on Wed Aug 03, 2005 7:04 am, edited 1 time in total.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Check first that the new page has session_start() at the top. If not there's obviously something else wrong!
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

The page doesn't have that, and perhaps I don't understand it fully (as I'm a neub at sessions) but I'll show you the chain of events:


1: mainbox_signup1.php (the first form, asks the questions and calls to join_checks1.php via the form)

2: join_checks1.php (session_start(); happens here. This does the checks and creates the session)

Now if that didn't pass the test, it goes back to:

3: mainbox_signup1.php, only this time, it does a:

Code: Select all

if ($_SESSION['registration_details']['stage_1_pass'] == "0")
{
//do stuff
}
and note that step 3 works, even though mainbox_signup1.php does NOT have a session_start();

After that little loop between the mainbox_signup1.php and join_checks1.php finally passes, it then moves onto...

A: mainbox_signup2.php (does the same as the first one, only different questions, again, no session_start(); )

B: join_checks2.php (does the same as join_checks1.php, only this time it simply adds more data to the session)

C: based on a pass or fail, mainbox_signup2.php reloads, note, without session_start();



Now, what I don't get, is my code APPEARS to be almost identical between page 1 (mainbox_signup1.php) and page 2 (mainbox_signup2.php) except mainbox_signup2 doesn't seem to be able to read the session data. 8O


I hope that was easy enough to follow and gives a clearer picture of what's happening on this end.

Thanks again :)

rob
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Well any page that you want to call or set a session on will have to have session_start() at the top.
Perhaps stage 3 is working because its taking the "0" to mean false.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

mmmm, ok then.
Here's my other problem, if I try and stick session_start(); at the start of that page it gives a headers already sent error as that page is embedded in another. What I mean is, EVERY page on my site that is displayed to the user has a header.php run first which contains my header of course ;), and the menu's etc.

The question now would be, if I stick session_start(); at the very start of my header.php and run it on EVERY page the site uses, will it cause me problems?

Taking into consideration that once the user has registered I want to delete that session and I will most likely want to start other ones in the sute at various places.

I presume also then (that being the case) that session_start(); can be used with many differently named sessions?

Thanks again, I really appreciate it.


rob
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

if you call session_start() more than once per page (remember that with a iframe there are 2 pages) then you will get a notice so make sure that you just call session_start() once which if you include a header.php page before any output for every page and make sure that session_start() is at the top then you are in the clear
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

worked a treat, thanks so much everyone :)

much appreciated
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Here is something that can be handy, it only starts a session if it hasn't been started already..

Code: Select all

if (isset($_SESSION)) session_start;
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

so does that mean I don't need session_start(); at the top of each and every page? only put it there if it's not set?

If that's the case I would still need to put

Code: Select all

if (isset($_SESSION)) session_start;
Last question, promise ;) !!!!!!!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Actually, i made a typo ;)

You should check if in the current run (the file and all the files that are included) you only call session_start once.

A way to test if session_start was called before, would be to test if $_SESSION exists.

Code: Select all

if (!isset($_SESSION)) session_start;
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

brilliant, thank you everyone, it's working a treat now :)
Post Reply