[英]Php session variables being lost after redirect
你好,我已经找了几个星期来解决这个问题。 我有一个登录代码,在输入信息以登录表单后,将信息发送到一个 php 页面,该页面运行一个包含检查该数据库并保存信息(如果匹配)的函数,然后返回 true。 php 页面然后重定向到运行登录检查功能的主页,但会话不在那里。
我已经确定我有 session_start(); 在所有页面的顶部,标题有一个 exit(); 在它之后,重定向是 header('Location: ../home.php'); 我检查了 phpinfo(); 我试过 session_write_close(); 和 session_regenerate_id(true);, session_regenerate_id(); iv 检查会话保存路径 /temp
会话保存所有直到重定向 iv 随处回显检查每个帮助站点和页面
这是登录
if (login($email, $password, $mysqli) == true) {
// Login success
session_write_close();
header('Location: ../home.php');
exit();
} else {
// Login failed
header('Location: ../signIn.php?error=1');
exit();
}
我将会话保存在登录函数中,当我在此处回显时,它们仍然存在,但在重定向后,我在 loginCheck 函数中回显,但什么也没有。
这是我使用的会话函数
function sec_session_start() {
$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();
}
这是我的 loginCheck dosent 如果没有会话,它会通过第一个
function login_check($mysqli) {
// Check if all session variables are set
echo $_SESSION['hello'];
echo $_SESSION['username'];
echo $_SESSION['user_id'];
echo $_SESSION['login_string'];
echo 'hello';
if (isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
//if ($_SESSION['Logging'] == "correct") {
echo 'hello';
$user_id = $_SESSION['user_id'];
$login_string = $_SESSION['login_string'];
$username = $_SESSION['username'];
// Get the user-agent string of the user.
$user_browser = $_SERVER['HTTP_USER_AGENT'];
if ($stmt = $mysqli->prepare("SELECT password
FROM company_users
WHERE id = ? LIMIT 1")) {
// Bind "$user_id" to parameter.
$stmt->bind_param('i', $user_id);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
if ($stmt->num_rows == 1) {
// If the user exists get variables from result.
$stmt->bind_result($password);
$stmt->fetch();
$login_check = hash('sha512', $password . $user_browser);
if ($login_check == $login_string) {
// Logged In!!!!
return true;
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
}
这是表格
<form class="form-horizontal" action="includes/signInValidation.php" method="POST" name='login_form'>
<div class="form-group">
<label class="col-lg-2 control-label ">Email</label>
<div class="col-lg-10">
<input class="form-control textBox" id="email" name='email' placeholder="Email" type="email">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label ">Password</label>
<div class="col-lg-10">
<input class="form-control textBox" id="password" nam='password' placeholder="Password" type="password">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label "></label>
<div class="col-lg-10">
<input class="btn btn-success" type="button" value='Login' onclick='formhash(this.form, this.form.password);' >
</div>
</div>
</form>
这是登录功能
function login($email, $password, $mysqli) {
// Using prepared statements means that SQL injection is not possible.
if ($stmt = $mysqli->prepare("SELECT id, username, password, salt
FROM company_users
WHERE email = ?
LIMIT 1")) {
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($user_id, $username, $db_password, $salt);
$stmt->fetch();
// hash the password with the unique salt.
$password = hash('sha512', $password . $salt);
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;
// 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);
return true;
//echo $_SESSION['username'];
//echo $_SESSION['user_id'];
//echo $_SESSION['login_string'];
// Login successful.
} else {
// Password is not correct
// We record this attempt in the database
$now = time();
$mysqli->query("INSERT INTO login_attempts(user_id, time)
VALUES ('$user_id', '$now')");
return false;
}
}
} else {
// No user exists.
return false;
}
//$_SESSION['check'] = 'hello';
}
}
登录验证
<?php
include_once 'db/dbConnect.php';
include_once 'functions.php';
sec_session_start();
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p']; // The hashed password.
if (login($email, $password, $mysqli) == true) {
// Login success
//$_SESSION['loggedIn'] = 'true';
session_write_close();
header('Location: ../profile.php');
exit();
} else {
// Login failed
header('Location: ../signIn.php?error=1');
exit();
}
} else {
// The correct POST variables were not sent to this page.
echo 'Invalid Request';
}
我自己也有这个问题。 解决方案最终是我没有使用带有 Location: 标头的绝对 URI 。
这样做而不是 '../home.php' :
header('Location: http://yourdomain.com/home.php');
使代码尽可能简单。 不需要回显来确认会话已设置。 您需要一起重新设计流程
<?php
session_start();
if ( !isset( $_SESSION['user_id'] ) )
{ header("location:index");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.