It looks like I still haven’t got the grasp on sessions.
Session data will simply not be accessible for included files or stored when page is reloaded.
I have the following code:
page1.php
<?php
/*
Template Name: Some template
*/
session_start();
$_SESSION['start'] = 'start';
print_r($_SESSION);
if(some condition)
include('include1.php');
else
include('include2.php');
?>
include1.php
<?php
/* Some comments here */
$_SESSION['test'] = 'Test text';
print_r($_SESSION);
?>
include2.php
<?php
/* Some comments here */
print_r($_SESSION);
?>
Page1 first includes include1.php where I do some stuff. Then I load page1 including include2.php.
The output result of the print_r() is:
Array ( [start] => start ) // From page 1
Array ( [start] => start [test] => Test text ) // From include 1
Array ( [start] => start ) // From include 2
My question is:
1) Why isn’t [include] outputted in page1.php in the first print_r()after reload?
2) Why isnt’ [include]outputted in include2.php?
I only add session_start() in page 1 since the other two files are included. I’ve also tried adding session_start() in both include files, but that doesn’t work either, since it creates new instances.
UPDATE
My “actuall” include code:
switch($action) {
case 'a': include_once('include/include1.php'); break;
case 'b': include_once('include/include2.php'); break;
//default:
}
print_r($_SESSION); // <- Here all registered session variables are outputed
The code you displayed is working the way it is supposed to:
You initialized sessions –
session_start()You created the first element in the array –
startOn the first include, you added an array element to $_SESSION by adding the key
testWhen you output the first include, it includes both
startandtestOn the second include, you did not add any array elements to $_SESSION, which is why it outputs only
startThe include code you stated only calls in the first page, or the second page, but not both.