PHP $-SESSION 变量在新页面上消失并且 return 为空
PHP $-SESSION variables disappear on new page and return empty
我对会话变量有疑问。自从我从 mysqli 切换到 PDO。它在 mysqli 上运行良好,但自从我切换到 PDO 后,这个问题就出现了。
我正在尝试登录,我有一个区域,我想确保用户只能看到,如果用户已登录。登录工作正常,但一旦我被提及我的索引文件,由于登录功能,我什么也看不到。我可以看到 $_SESSION 变量被填充,但是一旦我重定向到另一个文件,$_SESSION 变量就会消失并且我得到一个空数组:
Array
(
)
process_login.php
require_once('../inc/user.inc.php'); // here i have all my functions
$user = new User(); // New Instance of my User Class
$user -> sec_session(); // selfmade session function. I use start_session() in this function
if (isset($_POST['email'], $_POST['p'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$password = filter_var ($_POST['p'], FILTER_SANITIZE_STRING);
$login = $user -> login_user($email, $password);
if ($login) {
// Login sucessful
//print("<pre>".print_r($_SESSION,true)."</pre>"); //Here i get my $_SESSION variable printed out and it works. I see it is filled.
header('Location: ../index.php');
exit();
}
index.php
<?php
$title = 'Index';
$currentPage = 'Dashboard';
include('php/head.php');
require_once('../inc/user.inc.php');
$user = new User();
$user -> sec_session(); // here i call my session function again. Note: session_start() is included in this function
print("<pre>".print_r($_SESSION,true)."</pre>"); //Now The Array is empty?!?
?>
user.inc.php - sec_session 函数
protected function sec_session() {
$session_name = 'sec_session_id';
$secure = SECURE;
$httponly = true;
if (ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
exit();
}
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly);
session_name($session_name);
session_start();
session_regenerate_id();
}
登录时,我在登录函数中将 Session 设置为以下内容:
if ($db_password == $password) {
$user_browser = $_SERVER['HTTP_USER_AGENT'];
$user_id = preg_replace("/[^0-9]+/", "", $user_id);
$_SESSION['user_id'] = $user_id;
$username = preg_replace("/[^a-zA-Z0-9_\-]+/",
"",
$username);
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512',
$password . $user_browser);
return true;
}
上面的一切正常,但只是消失了,当我降落在我的 index.php 并且我知道有东西破旧,但我不知道它是什么。
您是使用 http 还是 https 访问您的本地站点?如果您使用安全 cookie PHP 将不会在 http 下读取它们,而是使用新 cookie 启动新会话,切换到 https 可以解决此问题。
从您的 sec_session 函数看来,您正在使用安全 cookie ($secure = SECURE;) 那么您的浏览器会话是否使用 HTTPS?如果您使用带有安全 cookie 的 HTTP,您将在每个页面上启动一个新会话,并且无法访问现有会话变量。
最好的解决方案是像访问实时网站一样使用 https 访问您的本地网站,但如果您关闭本地网站的安全 cookie 设置,该方法也能正常工作。
我对会话变量有疑问。自从我从 mysqli 切换到 PDO。它在 mysqli 上运行良好,但自从我切换到 PDO 后,这个问题就出现了。
我正在尝试登录,我有一个区域,我想确保用户只能看到,如果用户已登录。登录工作正常,但一旦我被提及我的索引文件,由于登录功能,我什么也看不到。我可以看到 $_SESSION 变量被填充,但是一旦我重定向到另一个文件,$_SESSION 变量就会消失并且我得到一个空数组:
Array
(
)
process_login.php
require_once('../inc/user.inc.php'); // here i have all my functions
$user = new User(); // New Instance of my User Class
$user -> sec_session(); // selfmade session function. I use start_session() in this function
if (isset($_POST['email'], $_POST['p'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$password = filter_var ($_POST['p'], FILTER_SANITIZE_STRING);
$login = $user -> login_user($email, $password);
if ($login) {
// Login sucessful
//print("<pre>".print_r($_SESSION,true)."</pre>"); //Here i get my $_SESSION variable printed out and it works. I see it is filled.
header('Location: ../index.php');
exit();
}
index.php
<?php
$title = 'Index';
$currentPage = 'Dashboard';
include('php/head.php');
require_once('../inc/user.inc.php');
$user = new User();
$user -> sec_session(); // here i call my session function again. Note: session_start() is included in this function
print("<pre>".print_r($_SESSION,true)."</pre>"); //Now The Array is empty?!?
?>
user.inc.php - sec_session 函数
protected function sec_session() {
$session_name = 'sec_session_id';
$secure = SECURE;
$httponly = true;
if (ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
exit();
}
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly);
session_name($session_name);
session_start();
session_regenerate_id();
}
登录时,我在登录函数中将 Session 设置为以下内容:
if ($db_password == $password) {
$user_browser = $_SERVER['HTTP_USER_AGENT'];
$user_id = preg_replace("/[^0-9]+/", "", $user_id);
$_SESSION['user_id'] = $user_id;
$username = preg_replace("/[^a-zA-Z0-9_\-]+/",
"",
$username);
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512',
$password . $user_browser);
return true;
}
上面的一切正常,但只是消失了,当我降落在我的 index.php 并且我知道有东西破旧,但我不知道它是什么。
您是使用 http 还是 https 访问您的本地站点?如果您使用安全 cookie PHP 将不会在 http 下读取它们,而是使用新 cookie 启动新会话,切换到 https 可以解决此问题。
从您的 sec_session 函数看来,您正在使用安全 cookie ($secure = SECURE;) 那么您的浏览器会话是否使用 HTTPS?如果您使用带有安全 cookie 的 HTTP,您将在每个页面上启动一个新会话,并且无法访问现有会话变量。
最好的解决方案是像访问实时网站一样使用 https 访问您的本地网站,但如果您关闭本地网站的安全 cookie 设置,该方法也能正常工作。