2016-04-22 31 views
0

昨天我已經發布了一些代碼,詢問用戶如何能夠通過表單更新密碼。看here更新密碼與PHP/MySQL的

但是更新密碼後,我無法登錄雖然我的Android應用程序。所以我決定改變一下forgotpassword.php文件。

<?php 
session_start(); 
require "../init.php"; 
ini_set('display_errors', 1); 



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


    $email = $_POST['email']; 
    $user_name = $_POST['user_name']; 
    $password = $_POST['user_pass']; 
    $passwordEncrypted = sha1($user_pass); 

    $confpassword = $_POST['confirm_pass']; 
    $confPasswordEncrypted = sha1($confirmPass); 

    if($password !== $confpassword){ 
     echo "<script>alert('Passwords are not equal')</script>"; 
    }else{ 
     $select_query = "SELECT * FROM user_info"; 

     $run_select_query = mysqli_query($con,$select_query); 

     while ($row_post=mysqli_fetch_array($run_select_query)){ 

       $_SESSION['id'] = $row_post['id']; 
       $user_id = $_SESSION['id']; 
       $useremail = $row_post['email']; 
       $username = $row_post['user_name']; 

       var_dump($user_id); 

      if($useremail == $email AND $username == $user_name){ 
       //echo "<script>alert('$useremail')</script>"; 
       //echo "<script>alert('$username')</script>"; 
       echo "<script>alert('$id')</script>"; 
       $update_posts = "UPDATE user_info SET user_pass='$passwordEncrypted',confirm_pass ='$confPasswordEncrypted' 
       WHERE $id='$_userid'"; 

       $run_update = mysqli_query($con,$update_posts); 
       //var_dump($user_name); 
      echo "<script>alert('Password Has been Updated!')</script>"; 
      }else{ 
      echo "<script>alert('No email or username was found')</script>"; 
      } 

     } 

    } 

} 
?> 

但是現在密碼沒有像以前那樣更新。更新語句或之前的行中有錯誤。 $ _SESSION ['id']不爲空,所以select查詢工作正常。

任何想法?

謝謝。

+1

您的代碼中沒有錯誤檢查。爲什麼2個SQL語句可以用1來實現(更新密碼和用戶匹配 - 如果它更改0行,用戶名或密碼無效)。 – symcbean

+0

單輪sha1不足以進行密碼散列。在這裏看到:http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords – 1615903

+0

請使用PHP的[內置函數(http://jayblanchard.net/proper_password_hashing_with_PHP.html )來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。確保你[不要逃避密碼](http://stackoverflow.com/q/36628418/1011527)或在哈希之前使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –

回答

2

錯字在where子句中。 WHERE $id='$_userid'";

變化更新查詢的WHERE子句這樣:WHERE $id='$user_id'";

1

更新您的選擇查詢:

$select_query = "SELECT * FROM user_info where email = '".$email."' and user_name = '".$username."' "; 

then check if mysqli_num_rows().如果> 0,則只能執行更新查詢&把數據會話。

而且您的更新查詢不proper.It應該是:

$update_posts = "UPDATE user_info SET user_pass='$passwordEncrypted',confirm_pass ='$confPasswordEncrypted' 
       WHERE $id='$userid'"; 
0

你的更新查詢應該是這樣的:

$update_posts = "UPDATE user_info 
    SET 
      user_pass='$passwordEncrypted', 
      confirm_pass ='$confPasswordEncrypted' 
    WHERE id = $user_id"; 
0

確定。我已經更改了代碼並使其工作。所以這就是我所做的。

1)我運行一個選擇查詢,以檢查用戶是否已經被註冊。如果是,則更新密碼並在他的電子郵件中發送新密碼。

2)如果沒有,那麼你在我的Android應用程序說,用戶的電子郵件中找不到JSON響應。

3)最後,用戶可以用他的更新5位數密碼:)登錄。

<?php 
require "init.php"; 
$email = $_POST['email']; 



    if($email){ 
     $select_query = "SELECT * FROM user_info"; 

     $run_select_query = mysqli_query($con,$select_query); 

     while ($row_post=mysqli_fetch_array($run_select_query)){ 

      $id = $row['id']; 
      $usermail = $row_post['email']; 
      $username = $row_post['user_name']; 



     } 
      if($usermail == $email){ 
       $don = array('result' =>"success","message"=>"user mail found."); 

       $random = rand(72891, 92729); 
       $new_pass = $random; 

       $email_password = $new_pass; 
       $new_pass = sha1($new_pass); 

       $update_pass = "update user_info set user_pass='$new_pass',confirm_pass='$new_pass' where user_name='$username'"; 

       $run_update = mysqli_query($con,$update_pass); 


        $subject = "Login information"; 

        $message = "Your password has been changed to $email_password"; 

        $from = "From: [email protected]"; 

        mail($email,$subject,$message,$from); 

       $don = array('result' =>"success","message"=>"your password has been updated. Please check your email"); 



      }else{ 
       $don = array('result' =>"fail","message"=>"user mail not found."); 
      } 
     }else{ 
      $don = array('result' =>"fail","message"=>"please enter your email"); 
     } 

    echo json_encode($don); 
?>