newbie help - Session variable not sticking

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
jeffduck
Forum Newbie
Posts: 6
Joined: Sun Jun 25, 2006 4:35 pm

newbie help - Session variable not sticking

Post 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.
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post 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.
Last edited by Clukey on Sun Jun 25, 2006 4:57 pm, edited 1 time in total.
jeffduck
Forum Newbie
Posts: 6
Joined: Sun Jun 25, 2006 4:35 pm

Post 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.
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

Thats fine. I just updated the post for your edit.
jeffduck
Forum Newbie
Posts: 6
Joined: Sun Jun 25, 2006 4:35 pm

Post 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?
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post 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 :D .
jeffduck
Forum Newbie
Posts: 6
Joined: Sun Jun 25, 2006 4:35 pm

Post 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!
Post Reply