2015-10-24 91 views
1

我正在創建登錄表單,個人檔案頁面和註銷,但是當我在頁面上收到錯誤的用戶名消息並單擊x按鈕時,它不會關閉。另外,當我只添加用戶名並單擊登錄按鈕時,頁面將變爲空白。有人能幫助我找出這裏的歪斜嗎?使用PHP和MySQL項目構建登錄頁面

<?php 

     /* 
      STEPS WE NEED TO TAKE... 

      1. Build Login HTML form 
      2. Check if form has been submitted 
      3. Validate form data 
      4. Add form data to variables 
      5. Connect to database 
      6. Query the database for username submitted 
      6.1 If no entries: show error message 
      7. Store basic user data from database in variables 
      8. Verify stored hashed password with the one submitted in the form 
      8.1 If invalid: show error message 
      9. Start a session & create session variables 
      10. Redirect to a "profile page" 
      10.1 Provide link to "logout" page 
      10.2 Add cookie clear to logout page 
      10.3 Provide link to log back in 
      11. Close the MySQL connection 
     */ 

     if(isset($_POST['login'])) { 

      // build a function to validate data 
      function validateFormData($formData) { 
       $formData = trim(stripslashes(htmlspecialchars($formData))); 
       return $formData; 
      } 

      // create variables 
      // wrap the data with our function 
      $formUser = validateFormData($_POST['username']); 
      $formPass = validateFormData($_POST['password']); 

      // connect to database 
      include('connection.php'); 

      // create SQL query 
      $query = "SELECT username, email, password FROM users WHERE username='$formUser'"; 
      //store the result 
      $result = mysqli_query($conn, $query); 

      // verify if result is returned 
      if(mysqli_num_rows($result) > 0) { 

       // store basic user data in variables 
       while($row - mysqli_fetch_assoc($result)) { 
        $user = $row['username']; 
        $email = $row['email']; 
        $hashedPass = $row['password']; 
       } 

       // verify hashed password with the typed password 
       if(password_verify($formPass, $hashedPass)) { 

        // correct login details! 
        // start the session 
        session_start(); 

        // store data in SESSION variable 
        $_SESSION['loggedInUser'] = $user; 
        $_SESSION['loggedInEmail'] = $email; 

        header("Location: profile.php"); 
       } else { // hashed password didn't verify 

        // error message 
        $loginError = "<div class='alert alert-danger'>Wrong username/password combination. Please try again.</div>"; 

       } 
      } else { // there are no results in database 

       $loginError = "<div class='alert alert-danger'>No such user in database. Please try again. <a class='close' data-dismiss='alert'>&times;</a></div>"; 

      } 

      // close the mysql connection 
      mysqli_close($conn); 
     } 

    ?> 

    <!DOCTYPE html> 

    <html> 

     <head> 

      <meta charset="utf-8"> 
      <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 

      <title>Login</title> 

      <!--Bootstrap CSS--> 
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 

       <!--HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries--> 
      <!--WARNING: Respond.js doesn't work if you view the page via file://--> 
      <!--[if lt IE 9]> 
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
       <![endif]--> 
     </head> 

     <body> 
      <div class="container"> 
       <h1>Login</h1> 
       <p class="lead">Use this form to log into your account</p> 
       <?php echo $loginError; ?> 

       <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> 
        <div class="form-group"> 
         <label for="login-username" class="sr-only">Username</label> 
         <input type="text" class="form-control" id="login-username" placeholder="username" name="username"> 
        </div> 
         <div class="form-group"> 
         <label for="login-password" class="sr-only">Password</label> 
         <input type="password" class="form-control" id="login-password" placeholder="password" name="password"> 
        </div> 
        <button type="submit" class="btn btn-default" name="login">Login!</button> 

       </form> 

      </div> 


    <!--Bootstrap JS--> 
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>  
     </body> 
    </html> 
+0

問題是數據庫中是否存在散列密碼以及列的類型是否正確和足夠長以容納散列。 10次​​中有9次,這就是問題所在。發佈數據庫模式將在這裏將猜測放在它之外。 –

+0

Fred,我的數據庫密碼的varchar設置爲255.到目前爲止,還沒有現有的散列密碼。我正在嘗試創建一個登錄頁面來在數據庫中創建一個。 –

+0

那你去那吧。沒有密碼,沒有登錄。用'password_hash()'創建一個。 –

回答

0

「弗雷德,在我的數據庫密碼的VARCHAR被設置爲255沒有現有的哈希密碼的呢。我試圖創建一個登錄頁面創建一個在數據庫中。 - 丹尼爾科爾特斯「

問題是,沒有在您的數據庫中進行比較散列密碼。

您將需要使用password_hash()函數創建一個。

旁註:

使用stripslashes(htmlspecialchars不防範SQL注入。這是最好用事先準備好的聲明

+0

@DanielCortes不客氣Daniel,* cheers * –

+0

使用準備好的聲明真的是「最好的」嗎? 'filter_var'&sanitizing是否符合您的規模? – David

+0

@David是的,涉及到數據庫。看看他們的'validateFormData()'函數。這對於防止SQL注入沒有太大作用。我寫的關於這個的東西,與'filter_var'沒有任何關係,完全是兩個不同的動物。 –

0

也是你的警報沒有消失,因爲引導依賴於jQuery的lib和你沒有導入它。

+0

謝謝jarguez,現在它的功能正在發生變化,新的挑戰是我創建的地址簿不會添加新的客戶端。 –