-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_password.php
More file actions
116 lines (94 loc) · 3.59 KB
/
Copy pathreset_password.php
File metadata and controls
116 lines (94 loc) · 3.59 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
106
107
108
109
110
111
112
113
114
115
116
<?php
// ini_set("session.cookie_lifetime", 1200);
session_start();
$errmessage = [];
if (!empty($_POST) && $_POST['authreset']) {
validate_reset();
}
function validate_reset()
{
global $errmessage;
$errmessage = [];
$data = [
"email" => FILTER_VALIDATE_EMAIL,
"password" => FILTER_SANITIZE_STRING
];
// filter all input
if ($s_data = filter_input_array(INPUT_POST, $data)) {
$path = "database/users.txt";
// create file handler
$handle = fopen($path, 'r+');
$found = false;
// Testing each input
foreach ($s_data as $key => $input) {
if (empty($input))
$errmessage[$key] = "Invalid input, Your $key is required";
}
// if their is no error message
if (empty($errmessage)) {
// if database folder and user.text has been created
if (file_exists($path) && filesize($path) > 0) {
$users = [];
//loop through each line in our database
while (!feof($handle)) {
$user = json_decode(base64_decode(fgets($handle)), true);
if (isset($user) && trim($s_data["email"]) == trim($user["email"])) {
// change Password
$user["password"] = $s_data["password"];
//if email Found
$found = true;
// header("location: login.php");
}
isset($user) ? ($users[] = $user) : "";
}
if ($found) {
file_put_contents($path, "");
// re write all datas to the file and lock the file while adding datas
foreach ($users as $this_user) {
file_put_contents($path, base64_encode(json_encode($this_user)) . "\r\n", LOCK_EX | FILE_APPEND);
}
$errmessage['general'] = "Password change successfully, kindly login with your new password! " . '<a href="login.php">here</a>';
} else {
// no match
$errmessage['general'] = "Invalid email, try again!";
}
fclose($handle);
} else {
die("Data Not Found!");
}
}
} else {
echo "<h1>Make sure you supplied all data!</h1>";
}
$_SESSION['errmessage'] = $errmessage;
}
include('header.php');
?>
<main>
<h1 align="center">Reset Password</h1>
<h1>
<?php
echo (isset($_SESSION['errmessage']['general']) ? $_SESSION['errmessage']['general'] : "");
unset($_SESSION['errmessage']['general']);
?>
</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
<div>
<label for="email">Email</label><br>
<input type="email" name="email" id="email" required>
<h3><?php echo isset($_SESSION['errmessage']) ? (isset($_SESSION['errmessage']['email']) ? $_SESSION['errmessage']['email'] : "") : "" ?></h3>
</div>
<br>
<div>
<label for="password">New Password</label><br>
<input type="password" name="password" id="password" required>
<h3><?php echo isset($_SESSION['errmessage']) ? (isset($_SESSION['errmessage']['password']) ? $_SESSION['errmessage']['password'] : "") : "" ?></h3>
</div>
<br>
<div>
<input type="submit" name="authreset" value="Change Password"><br>
</div>
</form>
</main>
</body>
</html>