PHP会话在登录时存在,但在其他页面上不存在


PHP Sessions Exist on log in but not on another page

所以我有一个登录脚本,它调用一个自定义用户类,用户登录并设置会话。在重定向到login.php页面上执行var转储可以确认用户已成功登录,并显示友好的欢迎消息。

设置会话的代码是:

private function setIsLoggedIn($credentials) {
    if (!isset($_SESSION)) {
        session_start();
    }
    $_SESSION['verified'] = $credentials;
    session_write_close();
}

对于login.php页面来说运行良好,函数的参数为:

// assign user info from db to the session
        $this->setIsLoggedIn(array('UserId' => $user->userid,
            'UserName' => $user->username,
            'RoleId' => $user->roleid));

这是login.php表单post-back

// if the form was submitted
if (isset($_POST['submit'])) {
// Set local variables from $_POST array elements (userlogin and userpassword) or empty strings
    $userLogin = (isset($_POST['userlogin'])) ? trim($_POST['userlogin']) : '';
    $userPassword = (isset($_POST['userpassword'])) ? trim($_POST['userpassword']) : '';
    //assign values to array and get user object
    $authArray = array('UserName' => $userLogin, 'Password' => $userPassword);
    $user = new Users($authArray);
    if ($user->getUserId() > 0) { //If credentials check out
        // redirect the user to SESSION value set from common class
        if (isset($_SESSION['prev_page']))
        {
            header('location:' . $_SESSION['prev_page'] );
            exit;
        }
        else
        {
            // Otherwise, assign error message to $msg
            $msg = '<p><strong>Thank your for logging in.<br  />You may now visit tools that are in development.</strong></p>';
        }
    } 
    else 
    {
        // Otherwise, assign error message to $msg
        $msg = '<p><strong>Sorry, that username and password are not recognized.<br  />Please try again.</strong></p>';
    }
}

现在我转到另一个页面并执行$var_dump($_SESSION);我总是得到其他东西,因为在页面中我设置了这个:

$_SESSION['prev_page'] = "http://".$_SERVER[SERVER_NAME]."/id/AphID/";

该文件的第一行是:

<?php
session_start();
var_dump($_SESSION); 

以下是我的PHP会话设置:

Session Support enabled
Registered save handlers    files user
Registered serializer handlers  php php_binary wddx
Directive   Local Value Master Value
session.auto_start  Off Off
session.bug_compat_42   On  On
session.bug_compat_warn On  On
session.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly Off Off
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_secure   Off Off
session.entropy_file    /dev/urandom    /dev/urandom
session.entropy_length  16  16
session.gc_divisor  1000    1000
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.hash_bits_per_character 5   5
session.hash_function   0   0
session.name    PHPSESSID   PHPSESSID
session.referer_check   no value    no value
session.save_handler    user    files
session.save_path   /tmp    /tmp
session.serialize_handler   php php
session.use_cookies On  On
session.use_only_cookies    Off Off
session.use_trans_sid   1   1

所以,基本上我的会话只真正存在于登录页面上,而没有其他地方,当我调用session_start()时;我总是有不同的课程。有什么想法吗?

session_write_close()在保存数据后结束会话。对session_start()的下一次调用将创建一个新会话。

如果您想在关闭会话后恢复会话,那么在关闭会话之前,您需要将会话id保存在某个位置。$sid = session_id()您可以将其保存在数据库或cookie中。

然后要恢复会话,您可以调用session_id($sid); session_start();