forked from jaiswalaman/Online-Notes-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatepassword.php
77 lines (69 loc) · 2.84 KB
/
updatepassword.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
//start session and connect
session_start();
include ('connection.php');
//define error messages
$missingCurrentPassword = '<p><strong>Please enter your Current Password!</strong></p>';
$incorrectCurrentPassword = '<p><strong>The password entered is incorrect!</strong></p>';
$missingPassword = '<p><strong>Please enter a new Password!</strong></p>';
$invalidPassword = '<p><strong>Your password should be at least 6 characters long and inlcude one capital letter and one number!</strong></p>';
$differentPassword = '<p><strong>Passwords don\'t match!</strong></p>';
$missingPassword2 = '<p><strong>Please confirm your password</strong></p>';
//check for errors
if(empty($_POST["currentpassword"])){
$errors .= $missingCurrentPassword;
}else{
$currentPassword = $_POST["currentpassword"];
$currentPassword = filter_var($currentPassword, FILTER_SANITIZE_STRING);
$currentPassword = mysqli_real_escape_string ($link, $currentPassword);
$currentPassword = hash('sha256', $currentPassword);
//check if given password is correct
$user_id = $_SESSION["user_id"];
$sql = "SELECT password FROM users WHERE user_id='$user_id'";
$result = mysqli_query($link, $sql);
$count = mysqli_num_rows($result);
if($count !== 1){
echo '<div class="alert alert-danger">There was a problem running the query</div>';
}else{
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
if($currentPassword != $row['password']){
$errors .= $incorrectCurrentPassword;
}
}
}
if(empty($_POST["password"])){
$errors .= $missingPassword;
}elseif(!(strlen($_POST["password"])>6
and preg_match('/[A-Z]/',$_POST["password"])
and preg_match('/[0-9]/',$_POST["password"])
)
){
$errors .= $invalidPassword;
}else{
$password = filter_var($_POST["password"], FILTER_SANITIZE_STRING);
if(empty($_POST["password2"])){
$errors .= $missingPassword2;
}else{
$password2 = filter_var($_POST["password2"], FILTER_SANITIZE_STRING);
if($password !== $password2){
$errors .= $differentPassword;
}
}
}
//if there is an error print error message
if($errors){
$resultMessage = "<div class='alert alert-danger'>$errors</div>";
echo $resultMessage;
}else{
$password = mysqli_real_escape_string($link, $password);
$password = hash('sha256', $password);
//else run query and update password
$sql = "UPDATE users SET password='$password' WHERE user_id='$user_id'";
$result = mysqli_query($link, $sql);
if(!$result){
echo "<div class='alert alert-danger'>The password could not be reset. Please try again later.</div>";
}else{
echo "<div class='alert alert-success'>Your password has been updated successfully.</div>";
}
}
?>