views:

125

answers:

2

In header.php I have:

<?php
if(!isset($_SESSION))
{
session_start();
}
?>

and further down I have:

$_SESSION[theme] = $_GET[theme];

Basicly there is a drop down box where the user selects the website theme, this sets the value of $_GET[theme] and I would like the selection to be remembered, however whenever the page is changed the theme resets to default.

header.php is the header file for every page - dont know if this is the problem.

If print_r($_SESSION) then the correct value is shown after Array [theme] => but if I click on a different page then Array [theme] => is reset to blank.

Please help me!

+9  A: 

What you probably intended in your header.php was something like this

session_start();

//initialise new session
if (!isset($_SESSION['theme']))
{
    $_SESSION['theme']='default theme';
}

//change theme if user requested it
if (isset($_GET['theme']))
{
    $_SESSION['theme'] = $_GET['theme'];
}

It looks like were setting $_SESSION['theme'] regardless of whether it is in the $_GET array.

Also note that I've used quotes around the array indexes - avoid using barewords for this purpose.

Paul Dixon
Along the right track - I think, however the print_r just show Array ( )
Add your entire header.php to the question.
Paul Dixon
+1 for adding ' on array indexes.
Mercer Traieste
I didn't notice that! Will highlight that...
Paul Dixon
A: 

Unintuitively, you have to start the session on EVERY PHP page on which you are using it. Hope that helps.

Antony Carthy