【发布时间】:2015-02-04 03:24:00
【问题描述】:
我已经搜索过,但似乎无法弄清楚这一点。我有一个 config.php 搜索活动会话,如果找到,则将用户通过,如果没有,则将其转发到 login.php 页面。 config.php 还获取原始 URL 并发布到 login.php,因此我们可以将它们重定向到它们最初要去的页面。
从那里开始应该很简单,进行身份验证,然后使用重定向变量将浏览器转发到原始页面。但它不是那样工作的。它将我转发回 login.php 并显示“对象已移动”。如果我把 header("location: /index.php");但如果我使用下面的 login.php 中的变量,则不会。
任何帮助将不胜感激!
PHP (config.php):
<?php
session_start();
// put somewhere in a config file
define('SESSION_EXPIRE',3600); // in seconds
// check passage of time, force log-out session expire time
if(isset($_SESSION['last_activity']) && (time() - strtotime($_SESSION['last_activity']) > SESSION_EXPIRE)) {
// destroy session
session_unset();
session_destroy();
}
// if user is logged in and unexpired, update activity
if(isset($_SESSION['user'])) {
// user is logged in
$_SESSION['last_activity'] = date('Y-m-d H:i:s');
}
// if user doesn't have session forward them to login page and post requested URL
if (!(isset($_SESSION['user']) && $_SESSION['user'] != '')) {
header ("Location: ../login.php?location=" . urlencode($_SERVER['REQUEST_URI']));
}
?>
PHP (login.php):
<?php
include("authenticate.php");
// check to see if user is logging out
if(isset($_GET['out'])) {
// destroy session
session_unset();
$_SESSION = array();
unset($_SESSION['user'],$_SESSION['access']);
session_destroy();
}
// get orginal URL from config.php
$url = $_GET['location'];
// check to see if login form has been submitted
if(isset($_POST['userLogin'])){
// run information through authenticator
if(authenticate($_POST['userLogin'],$_POST['userPassword']))
{
// authentication passed
header("location:".$url);
die();
} else {
// authentication failed
$error = 1;
}
}
// output logout success
if (isset($_GET['out'])) echo "Logout successful";
?>
HTML:
<div class="panel-body">
<form action="login.php" method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="userLogin" type="Username" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="userPassword" type="password" value="">
</div>
<!-- Change this to a button or input when using this as a form -->
<input class="btn btn-lg btn-success btn-block" type="submit" name="submit" value="Login" />
</fieldset>
</form>
</div>
【问题讨论】:
-
在 login.php 中也是
session_start();吗? -
那里也需要
-
@Fred-ii- 如果它在
authenticate.php中则不是 -
你碰巧用 IIS 运行 PHP 吗?我问是因为我只在我从事过的 .NET 项目中看到过 Object Moved。
-
标题
Location: X不是大写L加空格吗?
标签: php html redirect http-headers