重定向不使用标题(“位置:

时间:2014-12-05 21:05:17

标签: php html redirect http-headers

我已经搜索过,但似乎无法想出这个。我有一个config.php,它搜索一个活动的会话,如果找到则通过用户,如果没有,它会转到login.php页面。 config.php还抓取原始URL并发布到login.php,以便我们可以将它们重定向到最初的页面。

从那里它应该非常简单,进行身份验证,然后使用重定向变量将浏览器转发到原始页面。但它不是那样的。它将我转发回login.php并说“Object Moved”。如果我输入标题(“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>

2 个答案:

答案 0 :(得分:2)

我不确定我是否理解您的确切问题,但如果您尝试重定向到$ location并且它没有进入正确的页面或抛出错误,那么您可能需要在传递变量之前对其进行urldecode。

在您的配置中

编码URI:

// 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']));
}

因此,在您的登录解码中:

$url = urldecode($_GET['location']);

正如mGamerz所说,确保你的标题有一个国会大厦L和结肠后的空格

header("Location: ".$url);

答案 1 :(得分:1)

您需要从此处删除login.php:action="login.php"您正在丢失$ url变量,因为在该网页发回自身后,它未被包含在GET中。