2016-06-19 27 views
0

我想創建一個登錄頁面,但我不知道是什麼導致問題。我剛剛通過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> 
+2

*黑屏*意味着什麼壞了。添加這些行'error_reporting(E_ALL); ini_set('display_errors',1);'在PHP腳本的頂部,看它是否會產生任何錯誤。 –

回答

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>