按提交进入空白屏幕

时间:2016-06-19 20:49:17

标签: php html

我正在尝试创建一个登录页面,但我不确定是什么原因导致了问题。我刚读完PHP教程,所以它不是最好看的代码。这是代码。

登录页面:

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      .error {color: #FF0000;}
    </style>
    <title>Stock Manager Login</title>
  </head>

  <body>
    <?php
      function cleanInput($input) {
        $input = trim($input);
        $input = stripslashes($input);
        $input = htmlspecialchars($input);
        return $input;
      }
      $userErr = $passErr = $generalErr = "";
      $username = $password = "";
      $loginCheck[0] = false;
      $loginCheck[1] = false;
      $file = fopen("login.txt", "r") or die("Unable to open file.");

      $loginInfo[0] = fgets($file);
      $loginInfo[1] = fgets($file);
      fclose($file);

      if(empty($_POST["username"]))
        $userErr = "Please enter a username.";
      else
        $username = cleanInput($_POST["username"]);

      if($username != $loginInfo[0])
        $generalErr = "Invalid username/password.";
      else
        $loginCheck[0] = true;

      if(empty($_POST["password"]))
        $passErr = "Please enter a password.";
      else
        $password = cleanInput($_POST["password"]);

      if($password != $loginInfo[1])
        $generalErr = "Invalid username/password.";
      else
        $loginCheck[1] = true;

      if($loginCheck[0] == true && $loginCheck[1] == true)
        header('Location: admin.php');
    ?>

    <h1>Welcome to the Stock Manager</h1>

    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
      User: <input type="text" name="usermame" value="">
      <span class="error"><?php echo $userErr;?></span><br>
      Password: <input type="text" name="password" value="">
      <span class="error"><?php echo $passErr;?></span><br>
      <input type="submit" name="submit" value="Submit">
    </form>
    <p><span class="error"><?php echo $generalErr;?></span></p>
  </body>
</html>

登录信息:

user123
12345

管理页面(未完成):

<!DOCTYPE HTML>
<html>
  <head>
    <title>Stock Manager</title>
  </head>

  <body>
    <?php echo "<h2>Logged in as: $username</h2>"; ?>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

我认为问题可能是:

header('Location: admin.php');

在将任何代码发送到浏览器之前,必须添加所有php标头,在发送它们时,发送的代码太多。

试试这个:

    <?php
          function cleanInput($input) {
            $input = trim($input);
            $input = stripslashes($input);
            $input = htmlspecialchars($input);
            return $input;
          }
          $userErr = $passErr = $generalErr = "";
          $username = $password = "";
          $loginCheck[0] = false;
          $loginCheck[1] = false;
          $file = fopen("login.txt", "r") or die("Unable to open file.");

          $loginInfo[0] = fgets($file);
          $loginInfo[1] = fgets($file);
          fclose($file);

          if(empty($_POST["username"]))
            $userErr = "Please enter a username.";
          else
            $username = cleanInput($_POST["username"]);

          if($username != $loginInfo[0])
            $generalErr = "Invalid username/password.";
          else
            $loginCheck[0] = true;

          if(empty($_POST["password"]))
            $passErr = "Please enter a password.";
          else
            $password = cleanInput($_POST["password"]);

          if($password != $loginInfo[1])
            $generalErr = "Invalid username/password.";
          else
            $loginCheck[1] = true;

          if($loginCheck[0] == true && $loginCheck[1] == true)
            header('Location: admin.php');
        ?>

    <!DOCTYPE HTML>
    <html>
      <head>
        <style>
          .error {color: #FF0000;}
        </style>
        <title>Stock Manager Login</title>
      </head>

      <body>
    <h1>Welcome to the Stock Manager</h1>

    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
      User: <input type="text" name="usermame" value="">
      <span class="error"><?php echo $userErr;?></span><br>
      Password: <input type="text" name="password" value="">
      <span class="error"><?php echo $passErr;?></span><br>
      <input type="submit" name="submit" value="Submit">
    </form>
    <p><span class="error"><?php echo $generalErr;?></span></p>
  </body>
</html>
相关问题