我知道有很多關於此主題的帖子,但他們都沒有解決我的問題。PHP會話不會在頁面更改中保留價值
我有我的Windows計算機上運行的WAMP服務器。我創建了一個登錄系統,可以設置PHP會話並驗證他們在創建頁面時是否正在工作。一旦我改變到不同的頁面,無論是通過輸入URL或JavaScript函數,我失去了會話。
這裏是我的test.php的是登錄
require_once('config.php');
$user = new User();
$result = $user->login('[email protected]', 'mysecretpassword');
echo $result;
echo '</br>';
$result = $user->isLoggedIn();
echo $result;
該用戶是實際登錄
public function login($email, $password) {
// Hash Password
$password = $this->hashPassword($password);
// Check if email and password match
$query = "SELECT id, confirm_email FROM users WHERE email = ? AND password = ?";
$a_bind_params = array($email, $password);
$a_param_types = array('s','s');
$results = $this->db->select($query, $a_bind_params, $a_param_types);
// If we didnt get a result then email/password must be wrong
if(count($results) == 0) return 1;
// Now check that they verrified their email
if($results[0]['confirm_email'] == 'N') return 2;
// User is real and everything is good
// Update login Date
$a_bind_params = array(date('Y-m-d H:i:s'), $results[0]['id']);
$a_param_types = array('s','s');
$query = "UPDATE users SET login_at = ? WHERE id = ?";
// There was a problem updating their login table so just fail the login
if(!$this->db->update($query, $a_bind_params, $a_param_types)) return 3;
// Login user
Session::set("user_id", $results[0]['id']);
session_regenerate_id(true);
Session::set("login_fingerprint", $this->_generateLoginString());
return 0;
}
這裏,用戶的功能來檢查,如果用戶登錄功能in
// Checks if user is logged in
public function isLoggedIn() {
//if $_SESSION['user_id'] is not set return false
if(Session::get("user_id") == null)
return false;
$loginString = $this->_generateLoginString();
$currentString = Session::get("login_fingerprint");
if($currentString != null && $currentString == $loginString)
return true;
else {
//destroy session, it is probably stolen by someone
$this->logout();
return false;
}
}
這裏是創建會話的會話函數
public static function startSession() {
ini_set('session.use_only_cookies', true);
$cookieParams = session_get_cookie_params();
session_set_cookie_params(
$cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
SESSION_SECURE,
SESSION_HTTP_ONLY
);
session_start();
session_regenerate_id(true);
}
這些都是設定的功能/獲取用戶會話
public static function set($key, $value) {
$_SESSION[$key] = $value;
}
public static function get($key, $default = null) {
if(isset($_SESSION[$key]))
return $_SESSION[$key];
else
return $default;
}
最後,這是實際調用session_start()
,包括一些常量我的config.php文件
// REQUIRE ALL FILES
require_once("ClassSession.php");
require_once("ClassDatabase.php");
require_once("ClassUser.php");
Session::startSession();
如果我瀏覽到另一頁叫做test.php
include "config.php";
$user = new User();
if($user->isLoggedIn()) echo 'logged in';
else 'Not logged in';
會話丟失,用戶不再登錄。
我已經在PHP.ini中檢查了我的sessions.save_path並檢查了wamp64/temp文件夾,並且我的會話正在存儲在那裏。我也在每個頁面上調用session_start(),因爲我在兩個測試頁面都包含了config.php。不知道爲什麼我失去了我的會議。
編輯
我忘了說什麼是真正發生的事情。當我登錄用戶時,我從數據庫中查找他們的user_id並將其存儲到$_SESSION['user_id']
中。當我從登錄用戶的頁面訪問$ _SESSION [user_id
]時,我找回了正確的值。但是,當我更改爲另一頁$_SESSION['user_id']
爲空。
UPDATE
session_regenerate_id(true)
是沒有問題的。
請用[mcve] –
更改這一串代碼可能會錯過'session_start()'調用。 –
我不知道我怎麼可能使它變小。所有這些都是必需的。如果你想閱讀我的整篇文章,你會發現我在每一頁上調用'session_start()'。 –