php - 使用 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']
,但问题仍然存在。
解决方案
推荐阅读
- python - Python中的随机梯度下降
- amazon-web-services - S3 和 .net Amazon SDK - 无法下载文件
- sqlite - 不包括 sqlite 在哪个系统上
- python - 在 PySpark 中提取特定行
- python - 这个字符串在 seaborn 散点图的调色板参数中是什么意思?
- c - 有没有一种简单的方法可以在 FPGA 上运行的 C 程序中与网页进行交互?
- php - WebSocket 连接失败:连接建立错误:net::ERR_CONNECTION_TIMED_OUT
- tfs - 无法在 TFS 中查看集合或项目
- c++ - 建议重构以下形式的 if 语句?
- kubernetes - 在 Kubernetes 中指定每个资源类型请求?