2012-04-10 90 views
4

我正在编写一个基本的授权系统,我正在为此苦苦挣扎。有两个文件涉及 - index.phplogin.php。登录表单是非常简单的(这里面index.php): 带自定义头的PHP重定向

<fieldset class="right"> 
    <label for="email">Email 

     <input id="email" name="email" type="text" value=""/> 
    </label> 
    <label for="password">Password 
     <input id="password" name="password" type="password" /> 
     <a href="#" onclick="$('#password-box').toggle();" >Forgot your password?<span></span></a> 
    </label> 
    <div class="btn-login"><button type="submit" value="Login"></button></div> 
</fieldset> 
</form> 

login.php内容:

<?php 
// Include the launcher file. 
require_once('globals.php'); 
require_once(CORE . 'launcher.php'); 

// Collect the information sent to us. 
$mail = (isset($_POST['email'])) ? $_POST['email'] : ''; 
$password = (isset($_POST['password'])) ? $_POST['password'] : ''; 

$LoginError = false; 
    // AUTHORIZATION STUFF HERE 
if ($LoginError) { 
    header('Status: 200'); 
    header('X-Test: test'); 
    header('Location: index.php'); 
    exit(); 
} 

正如你所看到的,我送一个自定义标题包含在表单中的脚本在登录过程中发生错误。在index.php中,我使用的是headers_list(),但我要发送的标题不在列表中。

这是什么原因造成的?我试过.htaccess文件中的php_value "output_buffering" "0",但没有成功。

UPDATE 在Chrome中签入之后,浏览器正在接收标头,但在PHP中不可用。

在此先感谢。

+0

答案应该清楚,现在有四种不同的解释:D – 2012-04-10 18:58:48

+0

请参阅更新。 – Pateman 2012-04-10 19:00:32

回答

3

重定向发送到客户端,由客户端通过导航到给定的Location: index.php进行响应。您不应该期望浏览器在从服务器请求index.php时提供自定义标头。

CLIENT         SERVER 
|------- POST login.php ------------------>| 
|           | 
|<- 200, Location: index.php, X-Test:test -| (this is where you send the header) 
|           | 
|------- GET index.php ------------------->| (no header from client to server) 
1

我送一个自定义标题包含窗体

不,你不是脚本。你发送一个自定义头到客户端(用户的浏览器),客户端将简单地忽略/放弃它。

如果您需要维护状态,请使用Cookie /会话或在新位置放置某些内容,例如header('Location: index.php?login=false');

3

您在header()中指定的标头()将出现在server - > client的输出中。但是,在进行重定向时,浏览器将执行请求,客户端 - >服务器,并且浏览器没有义务在您的新请求中包含自定义标头。

1

正在接收您发送给用户的自定义标头。但是,当浏览器执行重定向时,它们将被丢弃,因为浏览器不会将它们发送到重定向页面。这就是为什么你不看他们。