-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforgot-password.php
285 lines (236 loc) · 7.55 KB
/
forgot-password.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
session_start();
require "mail.php";
$conn = mysqli_connect('localhost', 'root', '', 'userform');
if (!$conn) {
die("Connection failed".mysqli_connect_error());
}
$message='';
$message2='';
$mode = "enter_email";
if (isset($_GET['mode'])) {
$mode = $_GET['mode'];
}
//something is posted
if (count($_POST) > 0) {
switch ($mode) {
case 'enter_email':
$email = $_POST['email'];
//validate the email
if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$message = "Please enter a valid email";
} elseif (!valid_email($email)) {
$message = "Email entered was not found";
} else {
$_SESSION['forgot']['email'] = $email;
send_email($email);
header("Location: forgot-password.php?mode=enter_code");
die;
}
break;
case 'enter_code':
$code = $_POST['code'];
$result = code_authentication($code);
if ($result == "The Code is Correct") {
$_SESSION['forgot']['code'] = $code;
header("Location: forgot-password.php?mode=enter_password");
die;
}else{
$message = $result;
}
break;
case 'enter_password':
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if ($password != $cpassword) {
$message = "Passwords do not match";
} elseif(!isset($_SESSION['forgot']['email']) || !isset($_SESSION['forgot']['code'])){
header("Location: forgot-password.php");
die;
} else {
save_password($password);
if (isset($_SESSION['forgot'])) {
unset($_SESSION['forgot']);
}
header("Location: login.php");
die;
}
break;
default:
// code...
break;
}
}
function send_email($email){
global $conn;
$expire = time() + (60 * 10);
$code = rand(10000,99999);
$email=addslashes($email);
$query = "INSERT INTO resets (Email,Code,Expire)VALUES('$email','$code','$expire')";
$query_run = mysqli_query($conn,$query) or die("Could not update");
//send email
send_mail($email,'Password Reset',"Your code is " . $code);
}
function code_authentication($code){
global $conn;
$code = addslashes($code);
$expire = time();
$email = addslashes($_SESSION['forgot']['email']);
$query = "SELECT * FROM resets WHERE code = '$code' && email = '$email' ORDER BY id DESC LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result) {
if (mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
if ($row['Expire'] > $expire) {
return "The Code is Correct";
}else{
return "The Code Has Expired";
}
}else{
return "The Code You've Entered is Incorrect";
}
}
return "The Code You've Entered is Incorrect";
}
function save_password($password){
global $conn;
$password = sha1($password);
$email = addslashes($_SESSION['forgot']['email']);
$query = "UPDATE members SET password ='$password' WHERE email = '$email' LIMIT 1";
mysqli_query($conn,$query);
}
function valid_email($email){
global $conn;
$email = addslashes($email);
$query = "SELECT * FROM members WHERE email = '$email' LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result) {
if (mysqli_num_rows($result) > 0)
{
return true;
}
}
return false;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forgot Password</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
<style type="text/css">
.register-box
{
width: 50%;
margin-left: 28%;
margin-top: 50px;
background-color: #f5f5f5;
border-radius: 15px;
}
.wave
{
width: 250px;
border-radius: 30px;
}
p
{
color: #191970;
}
h1
{
margin: 5px;
}
.save
{
margin: 20px;
}
#error{
color: #8B0000;
}
</style>
</head>
<body>
<?php
switch ($mode) {
case 'enter_email':
?>
<div class="container">
<small id="emailHelp" class="form-text"><?php print("$message2");?></small><br>
<div class="row">
<div class="col-md-4 offset-md-4 form">
<div style="text-align: center;">
<img style="margin-top:10px;" src="./images/moaemblem.jpg" width="230" height="80" alt="AYUSH">
</div>
<form action="forgot-password.php" method="POST" autocomplete="">
<h2 class="text-center">Forgot Password</h2>
<br/>
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="Enter email address">
</div>
<div class="form-group">
<input class="form-control button" type="submit" name="check-email" value="Reset">
</div>
<div class="form-group">
<a href="login.php" style="text-decoration: none; color: black;"><i class="fa fa-arrow-left"></i> Back</a>
</div>
</form>
<?php
break;
case 'enter_code':
?>
<div class="register-box">
<h1 style="text-decoration: underline;" class="text-center">Code Verification</h1>
<p class="text-center">Enter the Code that was sent to your Email. <br/> <strong>Code Expires in 10 minutes!</strong><p>
<small id="emailHelp" class="form-text"><?php print("$message2");?></small>
<div class="text-center" id="error"><?php echo $message;?></div>
<br/>
<form action="forgot-password.php?mode=enter_code" method="POST">
<input style="width: 50%; margin-left:25%" type="text" class="form-control" name="code" placeholder="Enter Code"> <br>
<button style="width: 30%; margin-left:35%; color: white; background-color: blue;" type="submit" class="form-control button" name="SavePassChanges">Next</button>
<a href="forgot-password.php" class="login save">
<p style="width: 50%; margin-left:40%">Restart Reset</p>
</a>
</form>
<?php
break;
case 'enter_password':
?>
<div class="register-box">
<h1 style="text-decoration: underline; margin-top: 30px;" class="text-center">Reset Password</h1>
<br>
<small id="emailHelp" class="text-center"><?php print("$message2");?></small><br>
<form action="forgot-password.php?mode=enter_password" method="post">
<div class="form-group">
<input style="width: 60%; margin-left:22%; border-radius: 5px; height:40px;" type="password" class="text-center" name="password" placeholder="Enter New Password"> <br>
</div>
<div class="form-group">
<input style="width: 60%; margin-left:22%; border-radius: 5px; height:40px;" type="password" class="text-center" name="cpassword" placeholder="Confirm Password"> <br>
</div>
<br/>
<div class="form-group">
<button style="width: 20%; margin-left:40%; background-color: blue; color:white; border:1px; border-radius: 10px; height:30px;" type="submit" class="text-center" name="SavePassChanges">Continue</button>
</div>
<div class="form-group"></div>
<a href="forgot-password.php" class="login save">
<p style="width: 50%; margin-left:42%;" class="startover">Restart Reset</p>
</a>
</div>
</form>
</div>
<?php
break;
default:
// code...
break;
}
?>
</a>
</div>
</div>
</div>
</body>
</html>