Problem with $_SESSION variables

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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Problem with $_SESSION variables

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Problem with $_SESSION variables

Post by social_experiment »

If you want to use session variables you have to call session_start() at the top of the page.
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Problem with $_SESSION variables

Post 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?
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Problem with $_SESSION variables

Post 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??
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Problem with $_SESSION variables

Post 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'];
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Problem with $_SESSION variables

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