Page 1 of 1
newbie help - Session variable not sticking
Posted: Sun Jun 25, 2006 4:44 pm
by jeffduck
I'm trying to set a session variable with little luck. The variable is in an include. The parent file includes the session_start;. Also, I have a variety of session variables all around the site but don't have this problem with the others.
Here's the code
Code: Select all
<?php
echo $_SESSION['mastheadCounter'];
if( !isset($_SESSION['mastheadCounter'])){
$_SESSION['mastheadCounter']= 1 ;
} else {
$_SESSION['mastheadCounter']++ ;
}
echo $_SESSION['mastheadCounter'];
if( isset($_SESSION['mastheadCounter']) and
$_SESSION['mastheadCounter'] > 12 ) {
$_SESSION['mastheadCounter'] = 1;
}
?>
The first echo always results in an undefined variable error.
The second echo always results in 1.
And while I'm asking newbie questions... is there a way to avoid always checking if a session variable is set? I have a solution that has about 50 fields during a signup process and I'm getting lost in all the validation.
Thanks much for any guidance.
Posted: Sun Jun 25, 2006 4:52 pm
by Clukey
First before calling any session variables you must call session_start();.
The echo is returning an undefined error because you have not defined the variable yet do something like this:
Code: Select all
session_start();
$_SESSION['mastheadCounter'] = "0";
echo $_SESSION['mastheadCounter'];
This will return "0"
The second is returning 1 because if( !isset($mastheadCounter)){ is returning true therefore $mastheadCounter = 1.
Thirdly, So try this as your code:
Code: Select all
session_start();
$_SESSION['mastheadCounter'] = "0";
echo $_SESSION['mastheadCounter'];
if( !isset($_SESSION['mastheadCounter'])){
$_SESSION['mastheadCounter']= 1 ;
} else {
$_SESSION['mastheadCounter']++ ;
}
echo $_SESSION['mastheadCounter'];
if( isset($_SESSION['mastheadCounter']) and
$_SESSION['mastheadCounter'] > 12 ) {
$_SESSION['mastheadCounter'] = 1;
}
Also, use [ php ] and [ /php ] to post php code.
Posted: Sun Jun 25, 2006 4:56 pm
by jeffduck
Sorry! I edited the post to contain the correct code (Originally I took out the $_SESSION[] while testing)
As the code is posted, I'm expecting an error on the first echo, but on page refreshes, I continue to get the same result.
Posted: Sun Jun 25, 2006 4:59 pm
by Clukey
Thats fine. I just updated the post for your edit.
Posted: Sun Jun 25, 2006 5:29 pm
by jeffduck
Interesting... If I put this in a seperate file, everything works. If I put it either in my include file or directly in ANY of the parent files, it doesn't seem to remember that the variable is set and it always restarts.
Considering that I have other session variables that are working fine, could there be something else that would clear it?
Posted: Sun Jun 25, 2006 5:34 pm
by Clukey
session_unset() frees all the sesion variables and you should put that when you have finished with your session. But as for it not working in an included file, try using require() instead in include(). I don't know why this works, but it has for me before

.
Posted: Sun Jun 25, 2006 5:46 pm
by jeffduck
Well, if that ain't a lernin' experience...
I put an include in my include and it worked! Simply putting it directly in the first include didn't work
Then I tried putting it directly within the first include (as it was) and then as you recommended, I changed the reference from include_once to require_once. That also worked!
I guess that's one for good old fasioned experience.
Thanks much!