【问题标题】:php redirect when wrong password密码错误时php重定向
【发布时间】:2012-09-08 01:00:42
【问题描述】:

这是正确的,当一个人尝试访问它而不进入登录页面时,代码会将其重定向到登录页面

<?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 时遇到错误,可能因维护而关闭或配置不正确。”

【问题讨论】:

  • 您的代码中没有 test.html...您正在尝试做什么
  • 这看起来像您的结果页面,表单发布页面是什么样的?
  • 变量的传递是正确的,它的重定向方法我不确定。
  • 这与您的确切问题无关,但您至少应该只存储密码的 hash 并将其与 @987654323 的哈希值进行比较@。就目前而言,如果你犯了一个错误,导致 PHP 文件在没有被解析的情况下被发送,你的密码将被暴露给所有人看到。 (这至少发生在一个主要网站上,它肯定会发生在您身上。)更好的解决方案是使用数据库(即 SQL)进行身份验证,但这超出了您现在的问题范围。跨度>

标签: php html redirect login


【解决方案1】:

在您已经输出了一些 HTML 之后,您不能再调用 header。做你的密码检查和重定向。 HTML 上方

例如:

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

所以只有成功时才会显示 HTML。

【讨论】:

    【解决方案2】:

    您不能在向用户输出任何内容后发送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/");
        }
    ?>
    

    【讨论】:

      【解决方案3】:

      这样会更好:

      <?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>
      

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

      【讨论】:

        【解决方案4】:

        把 ob_start();在顶部和 o​​b_end_flush();这可能会解决它。

        【讨论】:

          【解决方案5】:

          在使用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>
          

          【讨论】:

            猜你喜欢
            • 2013-09-20
            • 1970-01-01
            • 2018-04-02
            • 1970-01-01
            • 2013-03-03
            • 1970-01-01
            • 1970-01-01
            • 2018-11-28
            • 1970-01-01
            相关资源
            最近更新 更多