如何跨页面使用存储和使用会话变量?

问题描述:

访问一页后,我想开始一个会话并存储一个会话变量:

When one page is accessed, I would like to start a session and store a session variable:

<?php
  session_start(); 
  $_SESSION['myvar']='myvalue';
?>

然后从另一页上,我要检查该会话变量是否已存储:

Then from another page, I would like to check if that session variable has been stored:

<?php
    session_start();
    echo("1");
    if(isset($_SESSION['myvar']))
    {
        echo("2");
       if($_SESSION['myvar'] == 'myvalue')
       {
           echo("3");
           exit;
       }
    }
    ?>

此代码对我不起作用.

根据对该问题的评论,似乎缺乏调整后的

Reasoning from the comments to this question, it appears a lack of an adjusted session.save_path causes this misbehavior of PHP’s session handler. Just specify a directory (outside your document root directory) that exists and is both readable and writeable by PHP to fix this.