changePassword.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. require_once('db_utils.php');
  3. if(isset($_POST['salt'], $_POST['new-password'])) { // Parameters received
  4. // Get parameters
  5. $old_salt = $_POST['salt'];
  6. $password = $_POST['new-password'];
  7. $conn = db_connect(); // Connect to the database
  8. // Check if the user already exists
  9. if(!($result = mysqli_query($conn, "SELECT * FROM user WHERE salt = '$old_salt'"))) {
  10. $verification = 'false'; // Wrong query
  11. }
  12. else { // Query successful
  13. if(mysqli_num_rows($result) != 1) { // User doesn't exist, or duplicated
  14. $verification = 'false';
  15. }
  16. else { // User returned successfully
  17. $algorithm = '$2a'; // Blowfish
  18. $cost = '$10'; // for hashing
  19. $new_salt = $algorithm . $cost . '$' . substr(sha1(mt_rand()),0,22);
  20. $hash = crypt($password, $new_salt);
  21. // Update activation status
  22. if(!mysqli_query($conn, "UPDATE user SET salt = '$new_salt', password = '$hash' WHERE salt = '$old_salt'")) {
  23. $verification = 'false'; // Wrong query
  24. }
  25. else { // Update successful
  26. $verification = 'true';
  27. }
  28. }
  29. }
  30. mysqli_free_result($result);
  31. db_close($conn); // Close the database
  32. }
  33. else { // Salt not been sent as parameter
  34. $verification = 'false';
  35. }
  36. header('Location: /index.html?changed='. $verification);
  37. ?>