-
Notifications
You must be signed in to change notification settings - Fork 0
/
forgotPassword.php
167 lines (151 loc) · 4.01 KB
/
forgotPassword.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
<?php include "../inc/dbinfo.inc"; ?>
<!DOCTYPE html>
<html>
<head>
<title>Forgot Password</title>
<style>
body {
background-color: #F2F2F2;
}
h1 {
font-size: 40px;
font-weight: bold;
text-align: center;
margin-top: 50px;
}
h2 {
text-align: center;
}
form {
background-color: white;
border: 2px solid #F2F2F2;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
margin: 50px auto;
padding: 20px;
width: 400px;
}
table {
margin-top: 20px;
margin-bottom: 20px;
width: 100%;
border-collapse: collapse;
}
td {
padding: 10px;
text-align: right;
vertical-align: middle;
border-bottom: 1px solid #ddd;
}
input[type="text"], input[type="password"] {
border: 1px solid #CCCCCC;
border-radius: 5px;
font-size: 16px;
height: 30px;
padding: 5px;
width: 100%;
}
input[type="submit"] {
background-color: #3e8e41;
border: none;
border-radius: 5px;
color: white;
cursor: pointer;
font-size: 16px;
height: 40px;
margin-top: 10px;
width: 100%;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
padding: 10px;
border-radius: 5px;
margin-right: 5px;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h2>Enter information below</h2>
<!-- Input form -->
<form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td>Current Password:</td>
<td><input type="password" name="OldPass"></td>
</tr>
<tr>
<td>New Password:</td>
<td><input type="password" name="NewPass"></td>
</tr>
<tr>
<td>Re-type Password:</td>
<td><input type="password" name="reNewPass"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Change Password">
</td>
</tr>
</table>
<p style="text-align:right">Back to Log In? <a href="https://ec2-54-82-112-252.compute-1.amazonaws.com/login.php">Click Here</a></p>
</form>
<?php
/* Connect to MySQL and select the database. */
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
$database = mysqli_select_db($connection, DB_DATABASE);
$username = htmlentities($_POST['Username']);
$oldPass = htmlentities($_POST['OldPass']);
$newPass = htmlentities($_POST['NewPass']);
$renewPass = htmlentities($_POST['reNewPass']);
if(strlen($username) && strlen($newPass) && strlen($oldPass) && strlen($renewPass)) {
if (strcmp($newPass,$renewPass)===0){
$us = mysqli_real_escape_string($connection, $username);
$ol = mysqli_real_escape_string($connection, $oldPass);
$ne = mysqli_real_escape_string($connection, $newPass);
if (checkPass($connection, $us, $ol)){
$query = "UPDATE user_details SET password='$ne' WHERE username = '$us';";
mysqli_query($connection,$query);
header("Location: login.php");
} else {
echo "<script>alert('Your current password is incorrect! Please try again!');</script>";
}
} else {
echo "<script>alert('Your new password does not match! Please try again!');</script>";
}
}
?>
<!-- Clean up. -->
<?php
mysqli_free_result($result);
mysqli_close($connection);
?>
</body>
</html>
<?php
function checkPass($connection, $username, $password) {
$u = mysqli_real_escape_string($connection, $username);
$p = mysqli_real_escape_string($connection, $password);
$query = "SELECT password FROM user_details WHERE username='$u';";
$result = mysqli_query($connection, $query);
$query_data = mysqli_fetch_row($result);
if(strcmp($query_data[0],$password)===0) {
return true;
} else {
return false;
}
}
?>