[SOLVED]variables N sessions

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
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

[SOLVED]variables N sessions

Post by blacksnday »

Im having a problem where if I have a variable for
a session, the session wont stick, but if
the session equals a variable it works fine.

Working:

Code: Select all

$_SESSION['site_template'] = $doselecttemplate;
Not Working:

Code: Select all

$_SESSION['site_template'] = $doselecttemplate;
$template = $_SESSION['site_template'];
Any ideas as to why?
Last edited by blacksnday on Mon Jan 02, 2006 1:37 pm, edited 1 time in total.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

what do you mean by "a variable for a session, the session wont stick"?

according to what you wrote,

all this should work...

Code: Select all

$_SESSION['site_template'] = $doselecttemplate;
$template = $_SESSION['site_template'];
echo ($doselecttemplate == $template) ? 'true' : 'false';
// that should return true.

EDIT: presuming session_start() is on.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

mickd wrote:what do you mean by "a variable for a session, the session wont stick"?
What I mean is that the below works:

Code: Select all

if($doselecttemplate){
	$_SESSION['site_template'] = $doselecttemplate;
}else if(!$_SESSION['site_template']){
	$_SESSION['site_template'] = 'default';
}
include("templates/$_SESSION[site_template]/header.php");
but the following wont work
because the $template is not containing the
session value of the selection.

Code: Select all

if($doselecttemplate){
	$_SESSION['site_template'] = $doselecttemplate;
                $template = $_SESSION['site_template'];
}else if(!$_SESSION['site_template']){
	$_SESSION['site_template'] = 'default';
                $template = $_SESSION['site_template'];
}
include("templates/$template/header.php");
LazyJones
Forum Newbie
Posts: 5
Joined: Tue Dec 27, 2005 8:56 pm

Post by LazyJones »

The only reason I can think of is that both if's fail and the $template doesn't have a value after that.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

the only reason i can see would be that session_start() isnt declared for the second one. if i would of guessed between which wouldnt of worked, i would of chose the first...
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Ok, i figured out what I was doing wrong.

$template should be outside the if/else

Code: Select all

if($doselecttemplate){
	$_SESSION['site_template'] = $doselecttemplate;
}else if (!$_SESSION['site_template']){
	$_SESSION['site_template'] = 'default';
}
$template = $_SESSION['site_template'];
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

ah, the scope of the variable, although i thought itll only give a notice and php would still work it accordingly? guess not.
Post Reply