forked from OnlineLibrary252Project/OnlineLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_password.php
More file actions
105 lines (67 loc) · 2.62 KB
/
Copy pathchange_password.php
File metadata and controls
105 lines (67 loc) · 2.62 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
$title="Change Password";
require_once 'template/header.php';
require_once 'config/database.php';
?>
<?php
if (isset($_SESSION['logged_in'])) {
header("location:". $config['app_url']);
die();
}
if ( !isset($_GET['token']) || !$_GET['token']) {
die("Token parameter is missing");
}
$timeNow = date("Y-m-d H:i:s");
$token = $_GET['token'];
$stmt = $mysqli -> prepare("SELECT * FROM reset_password WHERE token = ? AND expires_at > '$timeNow' ");
$stmt -> bind_param('s', $token);
$stmt -> execute();
$tokenValidation = $stmt->get_result();
// If number of rows was 0 then the ! symbol will make it 1 and page will die
if ( ! $tokenValidation-> num_rows ) {
die("Token is invalid");
}
$errors=[];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$password = mysqli_real_escape_string($mysqli,$_POST['password']);
$password_confirmation = mysqli_real_escape_string($mysqli,$_POST['password_confirmation']);
if(empty($password))array_push($errors,"Password is required");
if(empty($password_confirmation))array_push($errors,"Password Confirmation is required");
if(!count($errors) && $password != $password_confirmation)array_push($errors,"Password don't match");
if ( !count($errors) ) {
//$userExist = $mysqli -> query("select user_id,token from reset_password where token=' " . $_GET['token'] . " ' limit 1");
$userId = $tokenValidation->fetch_assoc()["user_id"];
$password = password_hash($password,PASSWORD_DEFAULT);
$mysqli -> query("UPDATE users SET password = '$password' WHERE id = '$userId' ");
// Log file
$log = "User_ID: ".$userId." has updated his password.";
logger($log);
//Delete previous tokens
$mysqli -> query("DELETE FROM reset_password WHERE user_id = '$userId' ");
header("location: login.php");
$_SESSION['success_message'] = "your password has been changed successfuly";
die();
}
}
?>
<div class="password_reset">
<h3 class="text-info">Create new Password</h3>
<hr>
<?php include 'template/errors.php'; ?>
<form action="" method="post">
<div class="form-group">
<label for="password">Password:</label>
<input class="form-control" type="password" name="password" placeholder="Your password" id="password">
</div>
<div class="form-group">
<label for="password_confirmation">Password Confirmation:</label>
<input class="form-control" type="password" name="password_confirmation" placeholder="Password Confirmation" id="password">
</div>
<div class="form-groupd">
<button class="btn btn-primary">Change Password</button>
</div>
</form>
</div>
<?php
require_once 'template/footer.php';
?>