Change Password


Whooo ^_^ now that you have your usersystem and all that other stuff you can now add things like change password and all that. That is what this tutorial is if you forgot to look at the title.

Okay you really only want 1 file and that is changepass.php

In that new file create something like:

PHP Code
  1. <?php
  2. session_start(); //allow sessions
  3. if ($logged[username]) //Check if logged in or now
  4. {
  5.  
  6. switch($_GET['action']) { //makes links ?action=
  7.  
  8. default:
  9. //form action and method
  10. echo "<form method='POST' action='changepass.php?action=change'>";
  11. //table with old pass, new pass and conferm pass
  12. echo "
  13. Old Password:
  14. <input type='password' name='oldpass' size='35'>
  15.  
  16. New Password:
  17. <input type='password' name='newpass' size='35'>
  18.  
  19. Conform Password:
  20. <input type='password' name='conpass' size='35'>
  21. ";
  22. //Submit button
  23. echo "<input type='submit' value='Change Password' name='update'></form>";
  24. break;
  25. case "change":
  26. //posts the old password and md5s it
  27. $oldpass = md5($_POST[oldpass]);
  28. //posts the new password and md5s it
  29. $newpass = md5($_POST[newpass]);
  30. //posts the conformation password and md5s it
  31. $conpass = md5($_POST[conpass]);
  32. //get the users old info from the database
  33. $info = mysql_query("SELECT * FROM `members` WHERE `username` = '$logged[username]'");
  34. $info = mysql_fetch_array($info);
  35. //if the old password matches the password in the database we continue
  36. if($info[password] == $oldpass) {
  37. //if the new password and conformation password are the same continue
  38. if($newpass == $conpass) {
  39. //Updates the new password into the database where username is logged in
  40. $update = mysql_query("UPDATE `members` SET `password` = '$newpass' WHERE `username` = '$logged[username]'");
  41. //displays message when updated
  42. echo "Password Updated, You will need to relogin with your new password.";
  43. //logs them out so they login with there new password
  44. @session_destroy();
  45. }else{
  46. //error message is the new password and the conformation password do not match
  47. echo "Your new password and conformation passwords do not match!";
  48. }
  49. }else{
  50. //error message is the old password does not match their database password
  51. echo "Your old password does not match the database password!";
  52. }
  53. break;
  54. }
  55. }else {
  56. //if the person is not logged in
  57. echo("You are not logged in!");
  58. }
  59. ?>


Hope you like it!
ShadowMage's Avatar
Author:
Views:
2,712
Rating:
Posted on Monday 26th March 2007 at 04:46 PM
Dean
Dean's Avatar
Also its still buggy becoz u did md5 its decrypted more than 1 time and ppl will get confused.
Posted on Thursday 15th March 2007 at 08:32 PM
DanielXP
DanielXP's Avatar
Updated.
Posted on Thursday 15th March 2007 at 08:25 PM
ShadowMage
ShadowMage's Avatar
eh can an admin please change that for me? ^^; forgot to change that one.
Posted on Thursday 15th March 2007 at 08:08 PM
Dean
Dean's Avatar
You said the table was users it was ment to be members!