$ _SESSION ['userid']在一个函数中工作,而不是下一个函数

gan*_*jan 2 php variables session global-variables

我不明白为什么这不起作用.首先我有一个这样的函数,用户名出现在:

if(isset($_POST['Commit'])){

if(empty($_POST['crime'])){
echo "You didn't select the type of crime you wish to do.";
}else{

   (...lots of code)

$name = $_SESSION['username'];`
Run Code Online (Sandbox Code Playgroud)

然后我有另一个功能,其中用户名显示为空白:

if(isset($_POST['action'])){

    (...)

if(empty($_POST['car'])){
echo "You didn't select a car.";
}else{


if($row['owner'] != $_SESSION['username']){
echo "This isn't your car.";
}else{
Run Code Online (Sandbox Code Playgroud)

我没有发布任何代码,但是你们得到了这个想法.为什么会这样?我认为$ _SESSION是全球性的并且始终可用

我的主要php文件看起来像这样:

require("php functions\page_functions.php"); 
require("php functions\gta_functions.php");

session_start();

class gtapage extends Page
{

        public function display()
        {

        displayGta();
        }
}
Run Code Online (Sandbox Code Playgroud)

其中displayGta(); 是我第一篇文章的功能

cee*_*yoz 5

session_start()被称为目前正使用的第二功能的脚本?$_SESSION仅适用于已打开会话的页面.