首页 > 解决方案 > 使用 PHP 登录后将用户重定向到上一页

问题描述

我试图在用户登录后将用户重定向到上一个页面。例如,如果用户在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'],但问题仍然存在。

标签: php

解决方案


推荐阅读