所以我有一个调用自定义用户类的登录脚本,用户登录并设置会话 . 在redict上对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表单的帖子
// 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.
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.
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 . 有任何想法吗?
1 回答
session_write_close()在保存数据后结束会话 . 对session_start()的下一次调用将创建一个新会话 .
如果要在关闭会话后恢复会话,则在关闭会话之前,需要将会话ID保存在某处 .
$sid = session_id()
您可以将其保存在数据库或cookie中 .然后恢复会话你可以调用
session_id($sid); session_start();