I have some problem understanding the behaviour of these scripts. What I want to do is to assign an array of values to a session variable from within a function like this:
<?php // functions.php
function authorise() {
$_SESSION['my_data'] = array(1 => FALSE, 2 => FALSE, 3 => FALSE, 4 => FALSE, 5 => FALSE);
echo "<p><a href=page1.php>Click to start</a></p>";
}
?>
Then from my index page I will call the function
<?php // index.php
include_once 'header.php';
authorise();
// PRINT OUT THE CONTENT OF $_SESSION['my_data']
if (session_status() == PHP_SESSION_NONE) {
echo "no session";
} else {
echo "session is on<br>";
echo print_r($_SESSION['my_data']);
}
echo "</body></html>";
?>
After having initialised the session in header.php
<!DOCTYPE html>
<html>
<body>
<?php session_start(); ?>
<?php include_once 'functions.php'; ?>
the script will correctly print
Click to start
session is on
Array ( [1] => [2] => [3] => [4] => [5] => ) 1
Yet when I reach page1.php
<?php // page1.php
echo "PAGE1<br>";
include_once 'header.php';
if (session_status() == PHP_SESSION_NONE) {
echo "no session";
} else {
echo "session is on<br>";
echo print_r($_SESSION['my_data']);
}
echo "</body></html>";
?>
variable $_SESSION['my_data']
is empty. The script prints
PAGE1
session is on
1