为什么php在不执行echo语句的情况下执行header语句


$count = mysqli_num_rows($result);  

if($count == 1){  
echo "<h1><center> Login successful </center></h1>";  
} else{  
echo "<script>alert('login failed! invalid username or password');</script>";
header("Location: index.php")  ;
}     

我想在登录失败时显示警告消息,如果用户按ok,则应再次返回登录页面。我试过上面的代码,但没有用。浏览器移动到登录页面而不显示警告消息。有其他办法吗?

echo运行良好,但代码在php中同步。这就是为什么使用echo运行头,但将您重定向到index.php和echo的头已经出现了一段时间。

如果SESSION:中存在消息,您可以将消息存储到SESSION并在index.php上显示

.
.
else{
session_start();
$_SESSION['msg'] = 'Login failed! invalid username or password';
header("Location: index.php");

index.php:

session_start();
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
unset($_SESSION['msg'];
}

最新更新