我有一个登录脚本,根据数据库检查用户凭据,如果它们是正确的,将为它们分配一个会话,然后将它们转发到主屏幕。
由于某些原因,它不会转发,但会很高兴地输出在它应该读取转发代码之后出现的文本。
<?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?"; }
}
}
?>
在header语句之前不应该向浏览器发送任何输出。你会得到报头已经发送错误(如果启用)
echo "This text echos no probelm";
来自PHP手册
请记住,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
你确定error_reporting是启用的吗?尝试删除所有echo语句并添加ob_clean();
要使它在您的代码中工作,有两个选项:
1)在require_once('scripts/includePDO.php');
(使能输出缓冲)之前的代码顶部添加ob_start()
或
2)从代码中删除echo "This text echos no probelm";