PHP会话存在于登录中,但不存在于其他页面上
因此,我有一个登录脚本,它调用一个自定义用户类,用户登录并设置会话。在redict to the login.php页面上执行var转储可确认用户已成功登录,并显示友好的欢迎消息 设置会话的代码是:PHP会话存在于登录中,但不存在于其他页面上,php,session,Php,Session,因此,我有一个登录脚本,它调用一个自定义用户类,用户登录并设置会话。在redict to the login.php页面上执行var转储可确认用户已成功登录,并显示友好的欢迎消息 设置会话的代码是: private function setIsLoggedIn($credentials) { if (!isset($_SESSION)) { session_start(); } $_SESSION['verified'] = $credentials;
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>';
}
}
此文件的第一行是:
<?php
session_start();
var_dump($_SESSION);
因此,基本上我的会话只存在于登录页面上,而不存在于其他地方,当我调用session_start()时;我总是有不同的课程。有什么想法吗 session\u write\u close()保存数据后结束会话。下一次调用session_start()将创建一个新会话
如果要在关闭会话后恢复会话,则在关闭会话之前,需要将会话id保存在某个位置$sid=session_id()
您可以将其保存在数据库或cookie中
然后,要恢复会话,可以调用
session\u id($sid);会话_start()代码>在哪里设置$\u会话['prev\u page']?如果在会话\写入\关闭调用后设置,则无效。如何重新打开此会话?
<?php
session_start();
var_dump($_SESSION);
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