好的。我有一个叫plans.php的页面,里面有三个链接(计划1,计划2,计划3)。每个链接都有自己的页面,它会重定向到登录页面(login.php,运行良好)。所以如果用户在点击"计划2"时没有登录,它会强制用户登录,这样他们就可以看到所需的页面,这完全取决于用户选择的"计划"。
问题:我很难将用户重定向回"所需计划(URL)"。
解决方案:如果用户选择"计划1或计划2(无论什么计划)",那么它将强制用户登录(我有那个工作正常),用户成功登录后,用户必须被重定向到他们各自的"计划页面"。
如果有任何熟悉这个问题,请帮助。
plans.php
<a href="plan-1.php">Plan 1</a>
<a href="plan-2.php">Plan 2</a>
<a href="plan-3.php">Plan 3</a>
plan-2.php
<?php
ob_start();
include "header.php";
if(!$current_user) {
require_login();
}
ob_end_flush();
?>
HTML代码:用户登录页面后将看到的内容。
<p>Hello, you have been redirected to "Plan 2"</p>
login.php
<?php
ob_start();
include "header.php";
if($current_user) {
req_logout(); }
ob_end_flush();
?>
HTML代码:
<form action="authenticate.php" method="POST">
<label for="email">Email</label><br/>
<input type"text" class="input" name="username" id="username" />
<label for="password">Password</label><br/>
<input name="password" type="password" class="input" id="password"/>
<input type="submit" value="Sign In" class="submit"/>
</form>
此文件验证登录表单提交到的用户凭据。
authenticate.php
<?php
session_start();
require_once "db.php";
db_connect();
require_once "auth.php";
$user_id = credentials_valid($_POST['username'], $_POST['password']);
if($user_id){
log_in($user_id);
if($_SESSION['redirect_to']){
header("Location: " . $_SESSION['redirect_to']);
unset($_SESSION['redirect_to']);
}else{
// Default page after user logs in.
header("Location: manage.php");
}
}else{
header("Location: login.php?error=1");
exit("You are being redirected");
}
?>
我在这个文件中有一些PHP函数。
auth.php
// Logs into the user $user
function log_in($user_id){
$_SESSION['user_id'] = $user_id;
}
// Returns the currently logged in user (if any)
function current_user(){
static $current_user;
if(!$current_user){
if($_SESSION['user_id']){
$user_id = intval($_SESSION['user_id']);
$query = "SELECT *
FROM `********`
WHERE `id` = $user_id";
$result = mysql_query($query);
if(mysql_num_rows($result)){
$current_user = mysql_fetch_assoc($result);
return $current_user;
}
}
}
return $current_user;
}
// Requires a current user (Restrict Access to Page)
function require_login(){
if(!$current_user){
$_SESSION['redirect_to'] = $_SERVER['REQUEST_URI'];
header('Location: signin.php');
exit("You must log in.");
}
}
2条答案
按热度按时间xdnvmnnf1#
尝试在用户单击计划链接时发送参数。传递或保存该参数,并在成功登录后使用该参数重定向到适当的页。
在plan-2.php中
**EDIT:**以下是使用通过
GET
和POST
发送参数的完整解决方案(正如我所要求的):plans.php
plan.php
signin.php
HTML代码:
authenticate.php
auth.php
ejk8hzay2#
由于一些流行的浏览器(如Chrome)缓存服务器重定向响应,如果您执行服务器重定向,请求的页面将始终重定向到浏览器遇到的第一个重定向页面。
要解决这个问题,你验证PHP页面应该包含以下重定向:
所以......是的,代码可能看起来有点蹩脚,但重要的是要记住不要使用http响应重定向。我尝试了所有可能的方法来禁用响应缓存,但chrome根本不在乎。我发现唯一安全的方法是使用javascript进行重定向。我没有尝试META http-equiv ="refresh"的方式。我想这是安全的,因为我们经常看到。
另一件要记住的事情是,如果用户没有登录,要避免呈现敏感的页面内容。
有了这个想法,你应该可以走了。
希望能有所帮助!