這個問題可能有點愚蠢,但我只是開始我的冒險與會議 - 不需要使用它們之前。在主頁上我有一個會話,可以正確存儲所有變量,這沒有問題。在其他頁面上使用會話變量
當我轉到同一個域下的子頁面並嘗試從會話中調用變量時,我只是得到空字段。我試圖做print_r($ _ REQUEST);在子頁面上打印出以下內容:
Array ([wp-settings-1] => libraryContent=browse&editor=html [wp-settings-time-1] => 1478015951 [PHPSESSID] => 0744bf21ab3712e4735e07d926433aa3 [sec_session_id] => 60e56049f51c76e9ec4932c6702e7b72)
它與主頁上的輸出相匹配。我知道我應該做一個session_start();但是當我包括在我的代碼我得到以下錯誤:
Notice: A session had already been started - ignoring session_start()
我知道變量沒有被通過的原因是因爲當我做
if(isset($_SESSION["user_id"])){
$user_id = $_SESSION["user_id"];
}
echo "User id: " .$_SESSION["user_id"];
我只得到
User id:
而且每當我嘗試調用該變量時,我都會收到一個錯誤信息,說明它沒有設置。
有沒有我失蹤的一步?
設置會話變量:
if ($stmt->num_rows == 1) {
// If the user exists we check if the account is locked
// from too many login attempts
if (checkbrute($user_id, $mysqli) == true) {
// Account is locked
// Send an email to user saying their account is locked
return false;
} else {
// Check if the password in the database matches
// the password the user submitted.
if ($db_password == $password) {
// Password is correct!
// Get the user-agent string of the user.
$user_browser = $_SERVER['HTTP_USER_AGENT'];
// XSS protection as we might print this value
$user_id = preg_replace("/[^0-9]+/", "", $user_id);
$_SESSION['user_id'] = $user_id;
$sessions['user_id'] = $user_id;
// XSS protection as we might print this value
$username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username);
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512', $password . $user_browser);
// Login successful.
return true;
} else {
// Password is not correct
// We record this attempt in the database
$now = time();
if (!$mysqli->query("INSERT INTO login_attempts(user_id, time)
VALUES ('$user_id', '$now')")) {
header("Location: ../error.php?err=Database error: login_attempts");
exit();
}
return false;
}
}
} else {
// No user exists.
return false;
,功能啓動安全會話:
function sec_session_start() {
$session_name = 'sec_session_id'; // Set a custom session name
$secure = SECURE;
// This stops JavaScript being able to access the session id.
$httponly = true;
// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
exit();
}
// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);
// Sets the session name to the one set above.
session_name($session_name);
session_start(); // Start the PHP session
session_regenerate_id(); // regenerated the session, delete the old one.
}
你在哪裏設置user_id進入會話?如果是wordpress使用global $ session; print_r($ sessions);它會打印你的會話變量。將user_id設置爲會話--- $ sessions ['user_id'] ='您的用戶ID';你可以訪問應用程序中的任何地方 –
不,我在登錄平臺時在php會話中設置user_id,與wordpress無關 $ _SESSION ['user_id'] = $ user_id; – ArtleMaks