为什么我的页面忽略了标题('Location:home /');码?

时间:2014-03-18 14:19:50

标签: php

我有一个登录脚本,用于检查数据库的用户凭据,如果它们正确,则会为它们分配一个会话,然后将它们转发到主屏幕。

出于某种原因,它不会转发,但会很乐意输出应该读取转发代码后出现的文本。

<?php
//require_once('scripts/include.php');
require_once('scripts/includePDO.php');

$error = '';
$form = $_POST['submit'];
$email = $_POST['email'];
$password = $_POST['password'];

if( isset($form) ) {
if( isset($email) && isset($password) && $email !== '' && $password !== '' ) {

$sql = "SELECT * FROM tbl_users WHERE email = :email and password = :password";

$q   = $conn->prepare($sql);
        $q->bindValue(':email',$email,PDO::PARAM_STR);
        $q->bindValue(':password',$password,PDO::PARAM_STR);
        $q->execute();

            $r = $q->fetch(PDO::FETCH_ASSOC);
            if(($r)!=0)

{ //success

$answer = $r['id'];

$_SESSION['logged-in'] = true;
$_SESSION['who'] = $answer;

echo "This text echos no probelm";

//If the login details are entered and they match those in the database, forward to new page.
header('Location: home/');

echo "Even this text will be shown";

exit;

// If information is wrong or missing, provide error message.
} else { echo "Sorry, something hasn't worked. Are you entering all the information correctly?"; }
} 

}

?>

4 个答案:

答案 0 :(得分:2)

在header语句之前不应该向浏览器发送输出。您将收到已发送的标头错误(如果已启用)

echo "This text echos no probelm";

答案 1 :(得分:1)

来自PHP manual

  

请记住,在任何实际输出之前必须调用header()   通过普通HTML标记,文件中的空行或PHP发送。

echo "This text echos no probelm";
//If the login details are entered and they match those in the database, forward to new page.
header('Location: home/');

删除echo

答案 2 :(得分:0)

您确定启用了error_reporting吗? 尝试删除所有echo语句并添加ob_clean();就在标题(...)之前

答案 3 :(得分:0)

要使代码正常工作,有两种选择:

1)在代码之前添加ob_start()require_once('scripts/includePDO.php');之前(启用输出缓冲)

或者

2)从代码中删除echo "This text echos no probelm";