这些是我的代码:
<?php
// Check connection
if ($conn->connect_error) {
$error = "Wrong Username and Password!";
$_COOKIE['err']=$error;
echo '<script language="javascript">';
echo 'alert("Wrong username or Password")';
echo '</script>';
header("Location: HomePage.php");
}else{
echo "Connected successfully";
$chckSelect ="SELECT DISTINCT intUsrType FROM trial1232016.tblUser WHERE strUsrName = '".$username."';";
$result = $conn->query($chckSelect);
if($result->num_rows > 0){
echo "ends here!";
}
?>
问题是:为什么这段代码不会生成警告错误的用户名或密码。它只是重定向到HomePage.php这是标题...为什么它立即指向那里没有显示错误的用户名或密码?任何人都可以帮助我吗?
答案 0 :(得分:1)
Echo已执行,但在您看到它之前 这条线
header("Location: HomePage.php");
导致之前回显的所有内容都不会被视为页面被重定向(重新加载)
要看到一些变化,你可以:
重定向到包含
的页面header("Location: HomePage.php?invalid");
然后在您的视图中检查是否设置了?invalid属性,然后显示消息
答案 1 :(得分:0)
// Check connection
if ($conn->connect_error) {
$error = "Wrong Username and Password!";
$_SESSION['err']=$error;
header("Location: HomePage.php");
}else{
echo "Connected successfully";
$chckSelect ="SELECT DISTINCT intUsrType FROM trial1232016.tblUser WHERE strUsrName = '".$username."';";
$result = $conn->query($chckSelect);
if($result->num_rows > 0){
echo "ends here!";
}
然后在你的HomePage.php
if ($_SESSION['err']){
echo '<script language="javascript">';
echo 'alert("Wrong username or Password")';
echo '</script>';
}