I'm trying to redirect user to the previous page after the user has logged in. For example, if the user in cart.php page and then go to sign.php page and sign in, it must automatically redirects to cart.php, how to do that? This is helpers.php code:
session_start();
function login_2($customer_id){
$_SESSION['SBCustomer'] = $customer_id;
global $db;
header('Location: index.php');
}
And here is sign_in.php code:
<?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>";
}
}
}
?>
I tried $_SESSION['current_page'] = $_SERVER['REQUEST_URI'] under $customer_id = $customer['id']; login_2($customer_id); and replaced header('Location: index.php'); with header("Location: ". $_SESSION['current_page']) still the problem exists. The other thing I tried is, I replaced header('Location: index.php'); with $_SERVER['HTTP_REFERER'] and still the problem exists.