0

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.

Algo
  • 1
  • 5
  • Else see through [similar previous questions](https://www.google.com/search?q=site:stackoverflow.com+redirect%20user%20to%20previous%20page%20after%20login%20php)… – mario Apr 04 '19 at 22:23
  • I've saw them but it didn't work... – Algo Apr 04 '19 at 22:25
  • 1
    "Didn't work" isn't a real problem description, nor enticing enough feedback for anyone to put much effort into an answer. – mario Apr 04 '19 at 22:29
  • You'll have to give us a better description of your problem than that before we can help you. Put some effort into a bit of debugging and narrow down the problem. Then if you believe its a different issue to what's discussed in the duplicate, please update us – ADyson Apr 04 '19 at 22:57
  • Ok, 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. – Algo Apr 04 '19 at 23:26
  • But what exactly is the problem? All you told us is that something doesn't work. What behaviour are you seeing exactly? An error? Some unexpected results? What debugging have you done? – ADyson Apr 05 '19 at 05:06

0 Answers0