我在理解这些脚本的行为时遇到了一些问题。我想要做的是从像这样的函数中为会话变量分配一个值数组:
<?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>";
}
?>
然后从我的索引页面我将调用函数
<?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>";
?>
在header.php
<!DOCTYPE html>
<html>
<body>
<?php session_start(); ?>
<?php include_once 'functions.php'; ?>
脚本将正确打印
Click to start
session is on
Array ( [1] => [2] => [3] => [4] => [5] => ) 1
然后我到达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>";
?>
变量$_SESSION['my_data']
为空。脚本打印
PAGE1
session is on
1