Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. //this script relies on a form that sends four variables: the username, current password, new password, and new password confirmation
  2.  
  3. include("connect.php"); //connect to db
  4. $salt = "asdf"; //salting is good
  5.  
  6. if (isset($_POST) && !empty($_POST)) //get variables from html form
  7. {
  8.     $username= $_POST['username'];
  9.     $cpass=$_POST['currentpassword'];
  10.     $pw1=$_POST['password1'];
  11.     $pw2=$_POST['password2'];
  12. }
  13. if(strlen($cpass) > 20 || strlen($pw1) > 20 || strlen($pw2) > 20) //check if longer than 20 chars
  14. {
  15.     setcookie("error", "Invalid input.", time()+3600);
  16.     header("location:somethingwentwrong.php");
  17.     die();
  18. }
  19. if($cpass == "" || $pw1 == "" || $pw2 == "") //make sure the two password fields aren't empty
  20. {
  21.     setcookie("error", "Invalid input.", time()+3600);
  22.     header("location:somethingwentwrong.php");
  23.     die();
  24. }
  25. if($pw1 != $pw2) //make sure the two new password fields match
  26. {
  27.     setcookie("error", "Invalid input.", time()+3600);
  28.     header("location:somethingwentwrong.php");
  29.     die();
  30. }
  31.  
  32. $cpass = md5($cpass.$salt); //hash the password then check the database to see if current password is correct
  33.  
  34. $sql="SELECT * FROM users WHERE username='$username' and password='$cpass'";
  35. $result=mysql_query($sql);
  36. $count=mysql_num_rows($result);
  37. if($count == 0)
  38. {
  39.     setcookie("error", "Invalid password input.", time()+3600);
  40.     header("location:somethingwentwrong.php");
  41.     die();
  42. }
  43. //if current pass is validated, continue
  44. $pw1 = md5($pw1.$salt);
  45. $query = "UPDATE users SET password = '$pw1' WHERE username='$creator'"; //update db
  46. mysql_query($query);
  47. setcookie("success", "Password successfully changed!", time()+3600);
  48. setcookie("pw", $pw1, time()+3600);
  49. header("location:success.php");
  50. die();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement