代码之家  ›  专栏  ›  技术社区  ›  treyBake user1850175

第2页未设置会话数据

  •  0
  • treyBake user1850175  · 技术社区  · 7 年前

    我创建了这个类来处理会话:

    # app/src/sessionHandler.php
    
    <?php
        namespace src;
    
        class sessionHandler
        {
            protected $_session;
    
            public function __construct()
            {
                session_start();
                $this->_session = $_SESSION;
            }
    
            public function getSession()
            {
                return $this->_session;
            }
    
            public function getSessionVar($key)
            {
                return $this->_session[$key];
            }
    
            public function setSessionVar($key, $value)
            {
                try {
                    $this->_session[$key] = $value;
                } catch (\Exception $e) {
                    require_once $_SERVER['DOCUMENT_ROOT']. '/app/src/errHandler.php';
    
                    $errHandler = new errHandler(5, $e->getMessage());
                    $errHandler->logErr();
                }
            }
    
            public function unsetSessionVar($key)
            {
                unset($this->_session[$key]);
            }
    
            public function destroySession()
            {
                session_destroy();
            }
        }
    

    我有page1.php,在这里可以:

    # app/build/page1.php
    
    <?php
        require_once $_SERVER['DOCUMENT_ROOT']. '/conf/env.php';
        require_once DOC_ROOT. '/conf/loader/class.php';
    
        $varHandler = new \src\varHandler;
    
        $sessionHandler = new \src\sessionHandler;
        $sessionHandler->setSessionVar('foo', 'bar');
    
        $foo = $sessionHandler->getSessionVar('foo');
        $varHandler->dumpVarR($foo);
    

    这将输出:

    倾倒:foo
    酒吧

    如预期。在page2.php上,我也尝试了同样的方法(没有设置):

    # app/src/build/page2.php
    
    <?php
        require_once $_SERVER['DOCUMENT_ROOT']. '/conf/env.php';
        require_once DOC_ROOT. '/conf/loader/class.php';
    
        $sessionHandler = new \src\sessionHandler;
    
        $session = $sessionHandler->getSession();
        $foo = $sessionHandler->getSessionVar('foo');
    
        $varHandler = new \src\varHandler;
    
        $varHandler->dumpVarR($foo);
        $varHandler->dumpVarR($session);
    

    哪些输出:

    注意:未定义索引:在线21中的FoI/VAR/WWW/Base/APP/SRC/SeSalthHANDLR.PHP

    倾倒:foo

    倾倒:
    阵列
    (
    )

    我不明白为什么会出错-不可能 /var/lib/php/session 权限错误,因为page1.php可以设置会话值。Page2似乎无法链接到同一会话。这样做:

    $sessionHandler->setSessionVar('foo2', 'bar2');
    $foo2 = $sessionHandler->getSessionVar('foo2');
    
    $varHandler->dumpVarR($foo2);
    

    在第2页工作,我可以看到bar2设置为foo2。我做错什么了?

    1 回复  |  直到 7 年前
        1
  •  0
  •   treyBake user1850175    7 年前

    答案与我的课有关,尤其是这一行:

    $this->_session = $_SESSION;
    

    如果我理解正确的话 __construct 不是我想的那样。每次初始化会话时,它都会重新声明 $this->_session 作为一个新的 $_SESSION -重写每页上的数据。

    我把班级改成:

    <?php
        namespace src;
    
        class sessionHandler
        {
            protected $_sessionId;
    
            public function __construct()
            {
                if (!isset($_SESSION)) {
                    $this->init();
                }
            }
    
            public function init()
            {
                session_start();
                $this->_sessionId = session_id();
            }
    
            public function getSessionVar($key)
            {
                return (isset($_SESSION[$key]) ? $_SESSION[$key] : 'no data found in session for key: '. $key);
            }
    
            public function setSessionVar($key, $value)
            {
                try {
                    $_SESSION[$key] = $value;
                } catch (\Exception $e) {
                    require_once $_SERVER['DOCUMENT_ROOT']. '/app/src/errHandler.php';
    
                    $errHandler = new errHandler(5, $e->getMessage());
                    $errHandler->logErr();
                }
            }
    
            public function unsetSessionVar($key)
            {
                if (isset($_SESSION[$key])) {
                    unset($_SESSION[$key]);
                } else {
                    return false;
                }
    
                return true;
            }
    
            public function destroySession()
            {
                try {
                    session_destroy();
                } catch (\Exception $e) {
                    require_once $_SERVER['DOCUMENT_ROOT']. '/app/src/errHandler.php';
    
                    $errHandler = new errHandler(5, $e->getMessage());
                    $errHandler->logErr();
                }
            }
        }
    

    所以它使用已经存在的 $会话 数据。现在第2页可以看到第1页声明的内容。