2014-10-08 201 views
0

目前,我的網站在數據庫中有一個包含用戶名和密碼的表。前端有2個文本框和一個登錄按鈕。一旦用戶填寫完這兩個字段並單擊登錄,它將檢查用戶名和密碼是否與數據庫相匹配,如果是,請讓他們進入並設置會話變量以檢查entier站點。這足夠安全嗎?我如何將其轉換爲表單身份驗證?我不想扔掉我的登錄表。如果我要將其轉換爲表單身份驗證,我還可以使用它嗎?任何人都可以指點我或告訴我如何做到這一點?由於自定義身份驗證以形成身份驗證

回答

0

我使用標準的登錄是somethig這樣的:

的index.html:

<form action="login.php" method="post"> 
      <table width="100%" border="0" style = "border-top: 2px solid #CCC; padding-top: 15px;"> 
       <tr> 
       <td><input type="text" class="text_box" placeholder="Usuario" name="user" id="user"></td> 
       </tr> 
       <tr> 
       <td><input type="password" class="text_box" placeholder="Contraseña" name="pass" id="pass"></td> 
       </tr> 
       <tr> 
       <td> 
        <?php 
        show_message(); 
        ?> 
        <input type="submit" value="Ingresar" class="boton_submit"> 
        </td> 
       </tr> 
      </table> 
     </form> 

,而PHP的...

<?php 
    if(isset($_POST["user"]) && isset($_POST["pass"])){ 
     include('conect_to_database.php'); 

     $user = $_POST["user"]; 
     $pass = $_POST["pass"]; 

     if(!filter_var($_POST["user"], FILTER_VALIDATE_EMAIL)) { 
      //Not a valid email ... sends back to login form 
      header('Location: index.php') ; 
      exit(); 
     } 

     $query = "SELECT id FROM user where user = ? and pass = ? LIMIT 1"; 
     $stmt = $mysqli->prepare($query); 
     $stmt->bind_param("ss", $user, $pass); 
     $stmt->execute(); 
     $stmt->bind_result($id); 

     while ($stmt->fetch()) { 
      //set your session variable 
      header('Location: ../../control_panel.php') ; 
      exit(); 
     } 
     //echo invalid user... sends back to login form 
     header('Location: index.php') ; 
    } 
?> 

我也建議檢查特殊用戶名和密碼都是字符。

此外,你不應該在你的數據庫中存儲密碼。您應該使用特殊算法存儲真實密碼的哈希值。

+0

是的我明白不把密碼存儲在數據庫中的文本我只想找出它是否安全,而不是表單身份驗證?兩者有什麼不同? – user3731575 2014-10-08 19:26:10