这个问题可能有点愚蠢,但我只是开始我的会话冒险 - 以前不需要使用它们。在主页上我有一个会话,它可以正确地存储所有变量,这没问题。
当我转到同一域下的子页面并尝试从会话中调用变量时,我只得到空字段。我试过做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.
}
答案 0 :(得分:0)
通过查看您的代码我明白它是wordpress网站,
如果是,请按照以下步骤操作:
步骤1:将以下代码添加到wp-content / themes-folder / functions.php
中$_SESSION['user_id'] = "your id";
第2步:设置user_id
if(isset($_SESSION['user_id'])) {
$value = $_SESSION['myKey'];
} else {
$value = '';
}
第3步:访问您的会话ID
add session_start() at header.php(or first line of your code)
then use your session variables.
如果您的网站不是wordpress
#!/bin/bash
foo=0
bar=foo;
eval "${bar}=1"
echo $foo;