2016-01-28 53 views
-1

我不確定這裏出現了什麼問題。用戶數據在我的MySQL數據庫中,並且是正確的。但是,當我嘗試登錄時,出現錯誤,提示用戶/密碼不正確。我正嘗試使用用戶的電子郵件地址登錄。另外我想將第一個名字和用戶標識添加到會話中。PHP註冊用戶名稱/密碼錯誤

<?php 
session_start(); 
include_once 'dbconnect_new.php'; 

if(isset($_SESSION['user'])!="") 
{ 
    header("Location: ../index.php"); 
} 

if(isset($_POST['btn-login'])) 
{ 
    $s_email = mysql_real_escape_string($_POST['email']); 
    $s_password = mysql_real_escape_string($_POST['password']); 

    $s_email = trim($s_email); 
    $s_password = trim($s_password); 

    $res=mysql_query("SELECT student_id, student_password, student_firstname FROM studentdata WHERE student_email='$s_email'"); 
    $row=mysql_fetch_array($res); 

    $count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row 

    if($count == 1 && $row['student_password']==md5($s_password)) 
    { 
     $_SESSION['user'] = $row['student_id']; 
     header("Location: ../index.php"); 
    } 
    else 
    { 
     ?> 
    <script> 
     alert('Username/Password Seems Wrong !'); 

    </script> 
    <?php 
    } 

} 
?> 
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 

     <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      <title>New Reg Page</title> 
      <link rel="stylesheet" href="style.css" type="text/css" /> 
     </head> 

     <body> 
      <center> 
       <div id="login-form"> 
        <form method="post"> 
         <table align="center" width="30%" border="0"> 
          <tr> 
           <td> 
            <input type="text" name="email" placeholder="Your Email" required /> 
           </td> 
          </tr> 
          <tr> 
           <td> 
            <input type="password" name="password" placeholder="Your Password" required /> 
           </td> 
          </tr> 
          <tr> 
           <td> 
            <button type="submit" name="btn-login">Sign In</button> 
           </td> 
          </tr> 
          <tr> 
           <td><a href="register_new.php">Sign Up Here</a></td> 
          </tr> 
         </table> 
        </form> 
       </div> 
      </center> 
     </body> 

     </html> 
+0

的mysql_query已被棄用。不要使用md5作爲密碼。也就是說,在$ res的定義之前var_dump $ s_email,並確保它是你期望的。 –

+0

爲什麼不在你的查詢字符串中使用'和'? –

+0

一些提示:$ _SESSION ['user']!=''刪除!=''部分,isset將返回true或false。現在在PHP中不推薦使用Mysql轉義,使用帶mysqli或PDO的預準備語句。 MD5/SHA加密不足以加密密碼並已被棄用,請改爲使用bcrypt。我從來沒有使用md5,但bcrypt爲相同的密碼創建不同的哈希。匹配密碼的唯一方法是password_verify()。這可能是你的情況的問題。你確定MD5在多次調用中爲相同的密碼生成相同的散列嗎? –

回答

0

試試這個代碼: -

$s_email = mysql_real_escape_string($_POST['email']); 
    $s_password = mysql_real_escape_string($_POST['password']); 
    $s_email = trim($s_email); 
    $s_password = md5(trim($s_password)); 

    $res=mysql_query("SELECT student_id, student_firstname FROM studentdata WHERE student_email='$s_email' AND student_password = '$s_password'"); 

    if (!$res) { 
     // Debug query result by below code 
     //echo 'Could not run query: ' . mysql_error(); 
     //exit; 
     echo '<script language="javascript">'; 
     echo 'alert("Username/Password Seems Wrong !")'; 
     echo '</script>'; 
    }else{ 
     $row = mysql_fetch_row($result); 
     $stu_id = $row[0];   
     $stu_fname = $row[1]; 
     $_SESSION['user'] = $stu_id; 
     header("Location: ../index.php"); 
    } 

希望這將幫助你:)

+0

謝謝你的完美工作。 – Lost

+0

不客氣:) –