Advertisement
Guest User

Untitled

a guest
May 4th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. <?php
  2. //Check authed
  3. require_once "auth.php";
  4. //Start session
  5. session_start();
  6.  
  7. //Include database connection details
  8. require_once('config.php');
  9.  
  10. //Array to store validation errors
  11. $errmsg_arr = array();
  12.  
  13. //Validation error flag
  14. $errflag = false;
  15.  
  16. //Connect to mysql server
  17. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  18. if(!$link) {
  19. die('Failed to connect to server: ' . mysql_error());
  20. }
  21.  
  22. //Select database
  23. $db = mysql_select_db(DB_DATABASE);
  24. if(!$db) {
  25. die("Unable to select database");
  26. }
  27.  
  28. //Function to sanitize values received from the form. Prevents SQL injection
  29. function clean($str) {
  30. $str = @trim($str);
  31. if(get_magic_quotes_gpc()) {
  32. $str = stripslashes($str);
  33. }
  34. return mysql_real_escape_string($str);
  35. }
  36.  
  37. //Sanitize the POST values
  38. $newpass = clean($_POST['newpass']);
  39. $repeat = clean($_POST['repeat']);
  40. $oldpass = clean($_POST['oldpass']);
  41. $user = clean($_POST['user']);
  42.  
  43. //Input Validations
  44. if($user == '') {
  45. $errmsg_arr[] = 'User ID missing';
  46. $errflag = true;
  47. }
  48. if($newpass == '') {
  49. $errmsg_arr[] = 'Newpass ID missing';
  50. $errflag = true;
  51. }
  52. if($repeat == '') {
  53. $errmsg_arr[] = 'Repeat pass ID missing';
  54. $errflag = true;
  55. }
  56. if($oldpass == '') {
  57. $errmsg_arr[] = 'Old Password missing';
  58. $errflag = true;
  59. }
  60. if($newpass != $repeat) {
  61. $errmsg_arr[] = 'Passwords dont match!';
  62. $errflag = true;
  63. }
  64.  
  65. //If there are input validations, redirect back to the login form
  66. if($errflag) {
  67. $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  68. session_write_close();
  69. exit();
  70. }
  71.  
  72. //Create query
  73. $qry="SELECT * FROM members WHERE login='$user' AND passwd='".md5($_POST['oldpass'])."'";
  74. $result=mysql_query($qry);
  75.  
  76. //Check whether the query was successful or not
  77. if($result) {
  78. if(mysql_num_rows($result) == 1) {
  79. //Login Successful
  80. $qry2 = "UPDATE members SET `passwd` = '".md5($_POST['newpass'])."' WHERE `login` = '$user'";
  81. $result2 = @mysql_query($qry2);
  82. }}
  83. //Check whether the query was successful or not
  84. if($result2) {
  85. header("location: pwchange-success.php");
  86. exit();
  87. }
  88. else {
  89. die("Its not working!");
  90. header("location: pwchange-form.php");
  91. }
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement