我正在尝试在用户登录后将用户重定向到上一页。例如,如果用户在cart.php页面中,然后转到sign.php页面并登录,则它必须自动重定向到cart .php,该怎么做?这是helpers.php代码:
session_start();
function login_2($customer_id){
$_SESSION['SBCustomer'] = $customer_id;
global $db;
header('Location: index.php');
}
这是sign_in.php代码:
<?php
session_start();
if(isset($_POST['submit_4'])) {
$email_2 = ((isset($_POST['email_3']))?sanitize($_POST['email_3']):'');
$password_2 = ((isset($_POST['password_3']))?sanitize($_POST['password_3']):'');
$sql_2 = $db->query("SELECT * FROM customers WHERE email ='$email_2'");
$customer = mysqli_fetch_assoc($sql_2);
$customerCount = mysqli_num_rows($sql_2);
if(empty($_POST['email_3']) && empty($_POST['password_3'])) {
$msg_2 = "<span class='text-danger'>Please enter your email address/password.</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
}
else if(empty($_POST['password_3']) && filter_var($_POST['email_3'],FILTER_VALIDATE_EMAIL)) {
$msg_5 = "<span class='text-danger'>Please enter your account password.</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
}
else if(!empty($_POST['email_3']) && !filter_var($_POST['email_3'],FILTER_VALIDATE_EMAIL)) {
$msg_2 = "<span class='text-danger'>Please enter a valid email address.</span>";
$msg_4 = "<span class='text-danger'>Please enter your account password.</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
} else if(empty($_POST['email_3']) && !empty($_POST['password_3'])) {
$msg_2 = "<span class='text-danger'>Please enter a valid email address.</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
} else {
if($customerCount > 0) {
if(password_verify($password_2, $customer['password'])) {
if($customer['isEmailConfirmed'] == 0) {
$msg_2 = "<span class='text-danger'>Please verify your email!</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
} else {
$customer_id = $customer['id'];
login_2($customer_id);
}
} else {
$msg_2 = "<span class='text-danger'>The email address and password combination you provided was not found. Please try again.</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
}
} else {
$msg_2 = "<span class='text-danger'>The email address is not registered in our system.</span>";
$msg_6 = "<span class='text-danger'>Please check the error(s) highlighted below.</span>";
}
}
}
?>
我在$_SESSION['current_page'] = $_SERVER['REQUEST_URI']
下尝试了$customer_id = $customer['id']; login_2($customer_id);
,并用header('Location: index.php');
替换了header("Location: ". $_SESSION['current_page'])
,但问题仍然存在。我尝试的另一件事是,我将header('Location: index.php');
替换为$_SERVER['HTTP_REFERER']
,但问题仍然存在。