php在密码错误时重定向


php redirect when wrong password

这是对的吗?当人们尝试使用访问登录页面时,代码会将其重定向到登录页面

<?php
$pass = 'password';
?>
<html>
<head>
<title></title>
</head>
<body>
<?php 
if ( $_POST["pass"] == $pass){
?>
Congrats you have log in!

<?php 
}else{
header("Location: http://signin.com/");
}
?>
</body>
</html>

我最后出现了"服务器错误网站在检索时遇到错误http://www.test.com它可能已停机进行维护或配置不正确。"

在输出一些HTML之后,不能调用header。进行密码检查&重新使用HTML 之上

例如:

<?php 
$pass = 'password';
if ( $_POST["pass"] != $pass){
    header("Location: http://signin.com/");
    exit;
}
?>
<html>
<head>
<title></title>
</head>
....

因此,HTML只有在成功的情况下才会显示。

在任何输出后都不能向用户发送header()

<?php
    $pass = 'password';
    if ( $_POST["pass"] == $pass)
    {
        ?>
        <html>
        <head>
        <title></title>
        </head>
        <body>
        Congrats you have log in!
        </body>
        </html>
        <?php 
    }
    else
    {
        header("Location: http://signin.com/");
    }
?>

这样的东西会更好用:

<?php
$pass = 'password';
if ($_POST["pass"] != $pass){
    header("Location: http://signin.com/");
    exit;
    }
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>

您需要检查用户是否已登录。如果未登录,请重定向并退出。如果是,则显示消息。

Put ob_start();在顶部和ob_end_flush();这可能会解决问题。

在使用header进行重定向之前,不能输出html。编码之前的所有逻辑:

<?php 
$pass = 'password';
if ($_POST["pass"] == $pass)
{
    $message = "Congrats you have log in!";
}
else
{
    header("Location: http://signin.com/");
}
?>  
<html>
<head>
<title></title>
</head>
<body>
    <?php echo $message; ?>
</body>