-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
217 lines (182 loc) · 6.66 KB
/
profile.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
<?php
session_start();
if (!isset($_SESSION['id']) || !isset($_SESSION['role'])) {
header('Location: login.php');
exit;
}
include 'connection.php';
include_once('includes/header.php');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `id`, `name`, `email`, `password`, `role` FROM `authentication` WHERE `id` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $_SESSION['id']);
$stmt->execute();
$result = $stmt->get_result();
$userData = $result->fetch_assoc();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password'])) {
$oldPassword = $_POST['old_password'];
$newPassword = $_POST['new_password'];
$confirmPassword = $_POST['confirm_password'];
// Validate old password
if ($oldPassword === $userData['password']) {
// Check if new password and confirm password match
if ($newPassword === $confirmPassword) {
// New password and confirm password match, proceed with password change
$updateSql = "UPDATE `authentication` SET `password` = ? WHERE `id` = ?";
$updateStmt = $conn->prepare($updateSql);
// Bind the new password directly
$updateStmt->bind_param("si", $newPassword, $userData['id']);
$updateStmt->execute();
$passwordChanged = true;
header('Location: ./profile.php');
} else {
$passwordMatchError = "New password and confirm password do not match.";
}
} else {
$passwordError = "Old password is incorrect.";
}
mysqli_close($conn);
}
?>
<style>
.container-center {
display: flex;
justify-content: center;
align-items: center;
}
form {
margin-top: 20px;
}
label {
display: block;
margin-bottom: 8px;
}
.card {
border: none;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: transform 0.2s;
}
.card:hover {
transform: scale(1.05);
}
.card-header {
background-color: #2d70aa;
color: #fff;
text-align: center;
font-size: 1.5rem;
}
.user-info {
padding: 20px;
}
.info-item {
margin-bottom: 15px;
}
.label {
font-weight: bold;
color: red;
margin-bottom: 5px;
}
.value {
color: #555;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
box-sizing: border-box;
}
button {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.success-message {
color: green;
margin-top: 10px;
}
</style>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header text-center">Profile</h1>
</div>
</div>
<div class="row">
<div class="container-center">
<div class="card col-lg-6">
<div class="card-header">
User Information
</div>
<div class="user-info container-center">
<div class="info-item">
<span class="label">Name:</span>
<span class="value"><?php echo $userData['name']; ?></span>
</div>
<div class="info-item">
<span class="label">Email:</span>
<span class="value"><?php echo $userData['email']; ?></span>
</div>
<div class="info-item">
<span class="label">Role:</span>
<span class="value"><?php echo $userData['role']; ?></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="container-center">
<div class="col-lg-8">
<form id="password-form" method="post" action="profile.php">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title text-center " style="font-weight: bold;">Update Password</h3>
</div>
<div class="panel-body">
<?php
if (isset($passwordChanged) && $passwordChanged) {
echo '<p class="success-message">Password changed successfully!</p>';
}
?>
<?php
if (isset($passwordError)) {
echo '<p class="error-message" style="color: red; font-weight: bold;">' . $passwordError . '</p>';
}
?>
<?php
if (isset($passwordMatchError)) {
echo '<p class="error-message" style="color: red; font-weight: bold;">' . $passwordMatchError . '</p>';
}
?>
<div class="form-group">
<label for="old_password">Old Password:</label>
<input type="password" name="old_password" id="old_password" class="form-control" required>
</div>
<div class="form-group">
<label for="new_password">New Password:</label>
<input type="password" name="new_password" id="new_password" class="form-control" required>
</div>
<div class="form-group">
<label for="confirm_password">Confirm Password:</label>
<input type="password" name="confirm_password" id="confirm_password" class="form-control" required>
</div>
<div class="form-group text-center">
<button type="submit" name="change_password" class="btn btn-primary">Change Password</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include_once('includes/footer.php');
?>