Guest User

Untitled

a guest
Oct 27th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4.  
  5. $user = $_SESSION['username'];
  6.  
  7.     if($user)
  8.     {
  9.             // User Logged in
  10.        
  11.             if ($_POST['submit']) {
  12.        
  13.                 // Check Fields
  14.                
  15.                 $oldpassword = $_POST['oldpassword'];
  16.                 $newpassword = $_POST['newpassword'];
  17.                 $repeatnewpassword = $_POST['repeatnewpassword'];
  18.                
  19.                 $oldpasswordHash = md5($oldpassword);
  20.                 $newpasswordHash = md5($newpassword);
  21.                 $repeatnewpasswordHash = md5($repeatnewpassword);
  22.                
  23.                 // Check password against the database.
  24.                
  25.                 // Connect db
  26.                 $connect = mysql_connect("localhost","root","");
  27.                 mysql_select_db("hub");
  28.                
  29.                 $queryget = mysql_query("SELECT password FROM users WHERE username='$user'") or die("Couldn't Change Password.");
  30.                 $row = mysql_fetch_assoc($queryget);
  31.                
  32.                 $oldpassworddb = $row['password'];
  33.    
  34.                 // Check password
  35.                     if ($oldpasswordHash==$oldpassworddb)
  36.                     {
  37.                        
  38.                         if ($newpassword==$repeatnewpassword)
  39.                         {
  40.                             if (strlen($newpassword)>25||strlen($newpassword)<6)
  41.                             {
  42.                                 echo "Password must be between 6 and 25 characters";
  43.                             }
  44.                            
  45.                             else {
  46.                             // Success
  47.                             echo "Success!!";
  48.                            
  49.                             // Change password in database
  50.                             $querychange = mysql_query("
  51.                                 UPDATE users SET password='$newpasswordHash' WHERE username='$user'
  52.                             ");
  53.                             session_destroy(); // end the old session with the old password to start a new session with the new password
  54.                             die("Your password has been changed. <a href='index.php'>Return to index</a>");
  55.                             }
  56.                            
  57.                         }
  58.                         else
  59.                             die("New passwords didn't match!");
  60.                     }
  61.                     else
  62.                         die("old password didn't match");
  63.             }
  64.             else{
  65.                 echo"
  66.                     <form action='changepassword.php' method='post'>
  67.                         <label>Old Password</label><input type='text' name='oldpassword'>
  68.                         <label>New Password</label><input type='password' name='newpassword'>
  69.                         <label>Repeat New Password</label><input type='password' name='repeatnewpassword'>
  70.                         <input type='submit' name='submit' value='Change Password'>
  71.                     </form>
  72.                     ";
  73.                 }
  74.     }
  75.     else
  76.         die("You must be logged in!");
  77. ?>
Add Comment
Please, Sign In to add comment