令人烦恼的是,我的登录检查脚本没有重定向到我告诉它的位置。我知道它正确地命中了if语句,因为我放了回声来检查它是否命中。它明目张胆地跳到标题,因为在第一种情况下,它会像应该的那样发出"-空白"的回声,然后还会发出"它到底走了这么远吗?"
因此,在此基础上,我知道它击中了头部——它只是简单地忽略了它,我一辈子都无法理解为什么。
我确信它很简单,我错过了一些明显得可笑的东西,但我就是看不见。有什么建议吗?
// makes sure they filled it in
if($_POST['frmLogin-username'] == "" || $_POST['frmLogin-password'] == ""){
echo " - blanks";
header('Location: ?page=landing&action=login&message=1');
echo "Did it even get this far?";
die;
}
else{
// checks it against the database
$query = mysql_query("SELECT * FROM shops WHERE shopUsername = '".$_POST['frmLogin-username']."'");
//Gives error if user dosen't exist
$count = mysql_num_rows($query);
if ($count == "0"){
echo " - no user";
header('Location: ?page=landing&action=login&message=2');
die;
}
else{
while($row = mysql_fetch_array( $query )){
//gives error if the password is wrong
if ($_POST['frmLogin-password'] != $row['shopPassword']){
echo " - wrong pass";
header('Location: ?page=landing&action=login&message=3');
die;
}
else{
// if login is ok then we add a cookie
$hour = time() + 3600;
setcookie(shopusername, $_POST['frmLogin-username'], $hour);
setcookie(shopid, $row['shopId'], $hour);
//then redirect them to the shop panel
header("Location: ?page=shop");
die;
}
}
}
}
编辑:这个问题与我在index.php中加载所有页面的方式有关,我现在正在调查includes我已经将这个流程页面移到了它自己的php文件中,它现在可以很好地进行
首先:您不能像Sam在评论中所说的那样,在使用echo
输出任何内容后发送标头。
其次,要发送重定向,Location:
之后的URL必须是绝对的,就像http://localhost/page/to/redirect/to.php
一样。
编辑
事实上,科尔宾在我的回答中击败了我大约10秒;-)
问题是我通过调用includes加载index.php中所有页面的方式,我现在正在调查includes。我已经将这个流程页面移动到了它自己的php文件中,它现在可以很好地进行
您可以在PHP中使用window.location
,只需使用echo
。