Php 如何在登录后将用户重定向到原始页面?
可能重复:Php 如何在登录后将用户重定向到原始页面?,php,redirect,login,Php,Redirect,Login,可能重复: 我正在尝试这样做,当一个未登录的用户浏览我的网站并点击一个链接时,他们会被带到登录页面(在jQuery lightbox窗口中打开),登录后,他们会被带回到以前/原来的页面 我已经尝试使用这个简单的代码来尝试实现这一点,但当我使用它时,它不会打开登录页面,直接进入上一页/原始页面,因此用户无法登录 if(!empty($_SERVER['HTTP_REFERER'])) { header('Location: '.$_SERVER['HTTP_REFERER']); } 以下
我正在尝试这样做,当一个未登录的用户浏览我的网站并点击一个链接时,他们会被带到登录页面(在jQuery lightbox窗口中打开),登录后,他们会被带回到以前/原来的页面 我已经尝试使用这个简单的代码来尝试实现这一点,但当我使用它时,它不会打开登录页面,直接进入上一页/原始页面,因此用户无法登录
if(!empty($_SERVER['HTTP_REFERER']))
{
header('Location: '.$_SERVER['HTTP_REFERER']);
}
以下是我的所有登录脚本代码:
HTML:
<form action="login.php" method="post" target="_top" >
<div class="row email">
<input type="email" id="email" name="email" placeholder="Email" value="<?php echo htmlentities($email); ?>" />
</div>
<div class="row password">
<input type="password" id="password" name="password" placeholder="Password" value="<?php echo htmlentities($email); ?>" />
</div>
<input type="submit" name="submit" class="submit-login" value="Login >
</form>
<?php
if (logged_in())
{
redirect_to("dashboard.php");
}
include_once("includes/form_functions.php");
// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('email', 'password');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$fields_with_lengths = array('email' => 30, 'password' => 30);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
$email = trim(mysql_prep($_POST['email']));
$password = trim(mysql_prep($_POST['password']));
$hashed_password = md5($password);
if ( empty($errors) ) {
// Check database to see if email and the hashed password exist there.
$query = "SELECT id, email, close_account ";
$query .= "FROM ptb_users ";
$query .= "WHERE email = '{$email}' ";
$query .= "AND password = '{$hashed_password}' ";
$query .= "AND close_account = '0' ";
$query .= "LIMIT 1";
$result_set = mysql_query($query);
confirm_query($result_set);
if (mysql_num_rows($result_set) == 1) {
// email/password authenticated
// and only 1 match
$found_user = mysql_fetch_array($result_set);
$_SESSION['user_id'] = $found_user['id'];
$_SESSION['email'] = $found_user['email'];
$_SESSION['sub_expires'] = $found_user['subscription_expires'];
$result = mysql_query("UPDATE ptb_users SET user_online='Online' WHERE id=".$_SESSION['user_id']."")
or die(mysql_error());
redirect_to("dashboard.php");
} else {
// email/password combo was not found in the database
$message = "<div class=\"infobox\"><strong>Email/Password combination incorrect.</strong><br />"
.= "Please make sure your caps lock key is off and try again.</div><br/>";
}
} else {
if (count($errors) == 1) {
$message = "<div class=\"infobox\">There was 1 error in the form.<div>";
} else {
$message = "<div class=\"infobox\">There were " . count($errors) . " errors in the form.<div>";
}
}
} else { // Form has not been submitted.
if (isset($_GET['logout']) && $_GET['logout'] == 1) {
$message = "<div class=\"infobox\">You are now logged out.</div>";
}
$email = "";
$password = "";
}
?>
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
当有人试图访问需要身份验证的页面时,请将该URL存储在$\u会话变量中。然后将它们重定向到登录页面。登录后检查$u会话变量是否存在。如果有,请将它们重定向回该URL。您可以在AJAX的帮助下登录,成功登录后只需重新加载页面。这将在许多方面更有帮助
e、 g.如果用户名/密码错误,则可以在查询lighbox窗口中看到错误,而不是等待页面加载显示错误。用户被重定向到登录页面,因为HTTP\u Referer将明显包含登录页面。请在表单中使用值为$\u SERVER['PHP\u SELF']的隐藏字段,登录后使用该字段重定向。