php 中的 session 只能在同一页面上工作,但不能在同一网站的其他页面上工作

php 中的 session 只能在同一页面上工作,但不能在同一网站的其他页面上工作

问题描述:

我有两个简单的页面(test.php、test2.php).

I have two simple pages(test.php, test2.php).

test.php

<?php
session_start();
$_SESSION['username'] = "wria";
echo '<a href = "test2.php">click to another page</a> <br>';

if($_SESSION['username']) {
echo $_SESSION['username'];
}
?>

test2.php

<?php
session_start();
var_dump($_SESSION);
if($_SESSION['username']) {
echo $_SESSION['username'];
}
?>

在输出中,当我打开 test.php,刷新后

in the output, when I open test.php, after a refresh

但是当我去test2.php时,输出出现:

but when I go to test2.php, the output appears:

没有显示.

注意:此文件位于真实主机上,而非本地.

Note: this file is on real hosting not local.

您保存会话的路径不可用.您必须将其更改为可用位置.你可以在php.ini中更改

your path to save session is unavailable. You have to change it for an available place. You can change in php.ini