2012-11-13 54 views
0

我有一個登錄表單,它在密碼輸入錯誤但電子郵件地址不正確時顯示一條消息。但是當電子郵件或兩者都不正確時,它不會顯示任何內容。我希望它在缺少這兩種情況下顯示消息(「密碼和電子郵件不匹配」)。我真的很讚賞任何幫助。登錄表單當電子郵件和密碼不匹配或電子郵件不存在時顯示消息

<div id="container"> 
      <div> 
       <h1>Log In</h1> 
       <form method="POST" action="logIn.php"> 
        <label>Email</label> 
        <input name= "txtEmail"/> 
        <label>Password</label> 
        <input type="password" name="txtPass"/> 
        <input type= "submit" name= "submit" value="Log In"/> 
       </form> 
      </div> 
     </div> 

<?php 

    require ('connection.php'); 

      if (isset($_POST["txtEmail"])) 
      { 
      $email= $_POST["txtEmail"];//NO RECONOCE LOS TEXT FIELDS!! 
      } 
      else 
      {$email= ""; 
      echo "Email empty";} 

      if (!empty($_POST['txtPass'])) 
      { 
      $pass= $_POST['txtPass']; 
      } 
      else 
      {$pass= ""; 
      echo "Password empty.";} 

      // Enviar consulta 
       $instruction = "SELECT Password, Email FROM customer WHERE Email = '". $email . "'"; 
       $query = mysql_query ($instruction, $connection) 
       or die ("Fallo en la consulta");  
      // Mostrar resultados de la consulta 
       $nrows = mysql_num_rows ($query); 
       if ($nrows > 0) 
       { 
        $result = mysql_fetch_array ($query); 
        if (isset($result['Password'])) 
        { 
        $clave = $result['Password']; 
        //echo "Isset password works."; 
        } 
        else {$result= "";} 
        Email doesnt exist, or ;} 


        if ($clave == $pass){ 
         header('Location: welcomeLogIn.php'); 
        } 
        else{ 
         echo "Password or Email doesnt match."; 
        } 
       } 
    mysql_close($connection); 
    ?> 
+2

錯在許多方面,聯合國哈希密碼,mysql_ *,聯合國sanatised輸入 – 2012-11-13 19:22:58

+1

您也不應該爲垃圾郵件發件人提供來自您網站的有效電子郵件。這就是爲什麼它總是「用戶名/密碼不匹配」。不要讓潛在的攻擊者獲得更多信息。 – thatidiotguy

+0

我知道它不夠安全。我只是開發基本的功能,所以然後我加起來的安全措施。我非常清楚你應該顯示一條消息,比如「電子郵件和密碼不匹配」,但是此刻我很沮喪地試圖在惡意登錄後顯示消息的代碼。 – Mijail

回答

0

感謝您提供您的代碼供我們審查。基於代碼,我可以看到你剛剛開始學習PHP。我已經用一種非常簡單的方法更新了你的代碼,來完成你所要求的。基本上,如果電子郵件或密碼爲空,那麼它將$ empty_value變量設置爲1.然後在腳本結尾檢查$ empty_value變量是否等於1.如果這是您希望的消息打印。請注意,您的代碼存在許多安全問題,建議您閱讀以下指南中的所有信息:http://phpsec.org/projects/guide/

<?php 

    require ('connection.php'); 
$empty_value = 0; 

      if (isset($_POST["txtEmail"])) 
      { 
      $email= $_POST["txtEmail"];//NO RECONOCE LOS TEXT FIELDS!! 
      } 
      else 
      {$email= ""; 
      $empty_value = 1;} 

      if (!empty($_POST['txtPass'])) 
      { 
      $pass= $_POST['txtPass']; 
      } 
      else 
      {$pass= ""; 
      $empty_value = 1;} 

      // Enviar consulta 
       $instruction = "SELECT Password, Email FROM customer WHERE Email = '". $email . "'"; 
       $query = mysql_query ($instruction, $connection) 
       or die ("Fallo en la consulta");  
      // Mostrar resultados de la consulta 
       $nrows = mysql_num_rows ($query); 
       if ($nrows > 0) 
       { 
        $result = mysql_fetch_array ($query); 
        if (isset($result['Password'])) 
        { 
        $clave = $result['Password']; 
        //echo "Isset password works."; 
        } 
        else {$result= "";} 
        Email doesnt exist, or ;} 


        if ($clave == $pass){ 
         header('Location: welcomeLogIn.php'); 
        } 
        else{ 
         echo "Password or Email doesnt match."; 
        } 
       } 
    mysql_close($connection); 
    ?> 

<?php 
if ($empty_value == 1) { 
echo "Password or Email doesn't match."; 
} 
?> 

    <div id="container"> 
      <div> 
       <h1>Log In</h1> 
       <form method="POST" action="logIn.php"> 
        <label>Email</label> 
        <input name= "txtEmail"/> 
        <label>Password</label> 
        <input type="password" name="txtPass"/> 
        <input type= "submit" name= "submit" value="Log In"/> 
       </form> 
      </div> 
     </div> 
+0

謝謝。但仍然沒有工作。當我輸入隨機電子郵件和隨機密碼時,不顯示任何消息。 – Mijail

+0

@Mijail,我只是對代碼進行了一些更改,請現在嘗試。 – thestepafter

0

如果沒有匹配的電子郵件,那麼你會得到任何結果,所以你可以簡單的添加:

if ($nrows > 0) 
{ 
    //You current code to check if the password is correct     
} 
else{ 
    echo "No matching email found"; 
} 
+0

Molte grazie。你的答案確實有效。儘管我不得不將php代碼切換到另一個名爲logIn3.php的文件,並將操作(表單)改爲重定向到該文件,因爲如果不是「找到未找到匹配的電子郵件」,則會在頁面首次加載時顯示。我只是說,如果有人瀏覽希望得到答案,並希望整個解釋。 – Mijail

相關問題