Page 1 of 1

Problem with $_SESSION variables

Posted: Wed Jan 26, 2011 9:35 am
by IGGt
Hi Guys,

I am trying to set my page to pass the configuration element as a SESSION variable, to save it having to reload from the database each time the page refresehes. But for some reason I can't get it to work.

I start off by checking for a particular SESSION Variable to check whether or not to load form SESSION variables, or to load form the database.

After this it carries out the necessary actions. Then at the end of the page I load everything back into SESSION variables to be carried over. But it isn't working.

Code: Select all

// TOP OF THE PAGE
if(isset($_SESSION['ConfRun'])){
    $ConfRun = $_SESSION['ConfRun'];
}else{
    $ConfRun = 1;
};
IF ($ConfRun == 0) {
		$config[] = array('sendEmail' => $_SESSION['sendEmail'],
                          'emailAddress' => $_SESSION['emailAddress'],
                          'pageRefresh' => $_SESSION['pageRefresh'],
                          'Update' => $_SESSION['vdUpdate'],
                          'Delays' => $_SESSION['esDelays'],
                          'timelargeH' => $_SESSION['timeLargeH'],
                          'timelargeM' => $_SESSION['timeLargeM'],
                          'LastLogin' => $_SESSION['LastLogin'],
                          'LastAction' => $_SESSION['LastAction'],
                          'LastSync' => $_SESSION['LastSync']
                                           );
}else{
 // RUN the MySQL Query, and load the contents into an array $config.
 // I know this section is OK, as it was used prior to introducing SESSION variables
};


//AT The end of the page
$_SESSION['sendEmail'] = $config['sendEmail'];
$_SESSION['emailAddress'] = $config['emailAddress'];
$_SESSION['pageRefresh'] = $config['pageRefresh'];
$_SESSION['Update'] = $config['Update'];
$_SESSION['Delays'] = $config['Delays'];
$_SESSION['timeLargeH'] = $config['timeLargeH'];
$_SESSION['timeLargeM'] = $config['timeLargeM'];
$_SESSION['LastLogin'] = $config['LastLogin'];
$_SESSION['LastAction'] = $config['LastAction'];
$_SESSION['LastSync'] = $config['LastSync'];
$_SESSION['ConfRun'] = 0;

Whenever I set $_SESSION['ConfRun'] = 0 it tells me that none of the variables have been set, and I get an error

Re: Problem with $_SESSION variables

Posted: Wed Jan 26, 2011 9:40 am
by social_experiment
If you want to use session variables you have to call session_start() at the top of the page.
Hth

Re: Problem with $_SESSION variables

Posted: Wed Jan 26, 2011 10:33 am
by IGGt
Because it is used as an include (inlude ('config.php');) on the main page, the SESSION has already been started. Adding session_start(); led to a message telling me it was being ignored.

It seems that the SESSION variables are carried over (when they exist), the config Array is created succesfully from the SESSION variables, but it is the bit at the end of the page that fails, where it puts them back into the SESSION array.

e.g. $_SESSION['sendEmail'] = $config['sendEmail'];

Is there something wrong with this statement?

Re: Problem with $_SESSION variables

Posted: Wed Jan 26, 2011 10:47 am
by IGGt
Now I am even more confused.

From my code:

Code: Select all

print_r ($config); //Line 76
results in:
[text]Array ( [0] => Array ( [sendEmail] => 0 [emailAddress] => name@email.com [pageRefresh] => 0 [Update] => 00:01:00 [Delays] => 00:05:00 [timelargeH] => 0 [timelargeM] => 12 [LastLogin] => 0 [LastAction] => 15 [LastSync] => 30 ) )[/text]

Code: Select all

$send = $config['sendEmail']; //Line 78
results in:
[text]Notice: Undefined index: sendEmail in C:\wamp\www\ts\config2.php on line 78[/text]

But I can see quite clearly that $config['sendEmail'] is right there!!! so how can it be undefined??

Re: Problem with $_SESSION variables

Posted: Wed Jan 26, 2011 10:58 am
by IGGt
OK, fixed one thing and created another problem.

for the config array created from the database I need to use

$_SESSION['emailAddress'] = $config['emailAddress'];

for the config array created from teh SESSION variables I need to use

$_SESSION['emailAddress'] = $config[0]['emailAddress'];

Re: Problem with $_SESSION variables

Posted: Thu Jan 27, 2011 8:45 am
by IGGt
Finally Resolved. As I was creating the $Config array in two places I was creating it differently in both places $config and $config[].

By removing the [] from the second one it now works fine.


cheers