Session 会话数据未传递到其他页面

Session 会话数据未传递到其他页面,session,session-variables,php,Session,Session Variables,Php,我面临的问题有两个方面: 我想使用会话包装器类作为创建 跨多个页面的会话 希望找到一种在设置超时后使上述会话过期的方法 作为一个示例用例,如果我的站点在访问页面之前需要身份验证,我将创建会话包装器的实例,如果用户的凭据有效,那么我将把它们重定向到帐户页面 // index.php if (invalidUser) { // Show error } else if(userIsValid($user_email, $user_pass)) { $sess = new Session(

我面临的问题有两个方面:

  • 我想使用会话包装器类作为创建 跨多个页面的会话
  • 希望找到一种在设置超时后使上述会话过期的方法
  • 作为一个示例用例,如果我的站点在访问页面之前需要身份验证,我将创建会话包装器的实例,如果用户的凭据有效,那么我将把它们重定向到帐户页面

    // index.php
    if (invalidUser) {
       // Show error
    } else if(userIsValid($user_email, $user_pass)) {
       $sess = new Session("MySite", 10);
       Utils::redirect("accountPage.php"); 
    }
    
    以下是重定向到“帐户”页面的实用程序方法:

    // utils.php
    ob_start(); // Start output buffer
    /**
      * Redirects the HTTP header to another location.
      *
      * @param (String) $address the new location to send the browser.
      */
      public static function redirect($address) {
         header("Location: $address");
         exit();
      }
    
    以下是会话包装器类的实现:

    // session.php
    class Session {        
           /**
             * Default Constructor.
             *
             * @param (String) $name the name of the session, as well as the session cookie name 
             * @param (String) $timeout the amount of time to permit the existence of 
             * this session.
             * -1, indicates that the session should live on indefinetely.
             */
            function __construct($name, $timeout = -1) {
                session_name($name);
                session_start();
    
                $_SESSION["timeout"] = $timeout;
                $_SESSION["created"] = time();
            }
    
        /**
         * Determines if the session is still considered "alive" based on its 
         * timeout + creation time.
         * If the session has expired we remove the session effectively "Timing out".
         */
        public static function isExpired() {
    
            // Default infinite timeout case
            if ($_SESSION["created"] == -1) {
                return false;
            }
    
            // Evaluate time left on session
            if(($_SESSION["timeout"] + $_SESSION["created"]) <= time()) {
                // Remove Session
                return true; 
            } else {
                // Session has not expired yet
                return false;
            }
        }
    }
    
    我知道它部分有效,因为如果我不重定向然后打印
    $\u会话
    全局
    数组,其中就有数据。我知道如何在每页的开头添加
    session\u start()
    ,但我想减少创建其他会话和cookie的工作量


    任何帮助都将是巨大的提前感谢

    在为每个页面发送输出之前,必须调用
    会话\u start()
    。对于“accountsPage.php”,您确实包含了“session.php”文件,但该文件仅定义了类,除非您有
    新会话(“MySite”,10),否则它实际上不会调用此函数页面代码中的某个地方


    我建议您实际删除
    session\u name($name)
    会话_start()\uu构造
    ,并将它们放在“session.php”脚本的顶部。显然,您需要将
    $name
    替换为
    “MySite”
    (硬编码),但是有办法解决这个问题。

    您的
    会话::isExpired
    返回
    FALSE,这是因为找不到请求的$\u会话的索引,并且它不在同一目录下

    比如说,在第一页,你调用了
    newsession('MyWebsite',10)。在其他页面中,在启动会话并获取$\ u会话值之前,需要调用MyWebsite会话名称

    若开发人员并没有指定需要调用哪个会话名称,那个么每个新请求的会话名称都将重置为默认名称。这就是为什么它将返回
    null
    。我正在稍微修改你的代码

    function __construct($name, $timeout = -1) {
       session_name($name);
       session_start();
    
       if(!isset($_SESSION['created'])) {
          $_SESSION["timeout"] = $timeout;
          $_SESSION["created"] = time();
       }
    }
    
    public function isExpired() {
       /* your code here */
    }
    
    我从isExpired()中获取了static,static没有调用类构造函数。第二页的示例

    <?php
    include('session.php');
    
    $session = new Session('MyWebsite', 10);
    $session->isExpired();
    print_r($_SESSION);
    

    这很好,但是有没有一种方法可以命名在创建“session.php”实例时创建的会话cookie?
    
    <?php
    include('session.php');
    
    $session = new Session('MyWebsite', 10);
    $session->isExpired();
    print_r($_SESSION);