无法从另一个文件访问PHP $ GLOBALS vars

I need to store persistent login information across connections, pages and sessions, so I cannot use $_SESSION. I read the docs about $GLOBALS:

引用全局范围内的所有可用变量

我尝试了这个:

index.php

<?php
$GLOBALS["level"] = "user";
$GLOBALS["time"] = time();
header("Location: /session.php");

session.php

<?php
$time = $GLOBALS["time"];
$level = $GLOBALS["level"];

var_dump($time);
var_dump($level);

输出:

NULL NULL

我不明白为什么它不起作用。我没有在函数内使用全局变量,因此处于“全局”范围内。

评论
  • 小小乖
    小小乖 回复

    You redirect to session.php because of your header. You add no data to it, just a Location header. This is the same as if you would open example.com/session.php in your browser (so no data). The $GLOBALS resets every time you refresh the page, just like a normal variable.

    Could it be you're looking for include ?

    另外,请尽量不要使用全局变量。我建议您阅读函数并使用那些传递的信息