-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_password.php
155 lines (134 loc) · 5.01 KB
/
change_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
<?php
session_start();
// Check if required parameters are present
if (!isset($_GET['id'], $_GET['type'], $_GET['course'])) {
echo "Invalid request";
exit;
}
$type = $_GET['type'];
$id = $_GET['id'];
$course = $_GET['course'];
$table = '';
$usernameadmin = '';
// Determine the table and validate session
switch ($type) {
case 'student':
if (isset($_SESSION['username']) || isset($_SESSION['studentusername'])) {
$table = 'student_info';
} else {
echo "Invalid request";
exit;
}
break;
case 'admin':
if (isset($_SESSION['username'])) {
$table = 'admin_info';
$usernameadmin = $_SESSION['username'];
} else {
echo "Invalid request";
exit;
}
break;
case 'examiner':
if (isset($_SESSION['username']) || isset($_SESSION['examinerusername'])) {
$table = 'examiner';
} else {
echo "Invalid request";
exit;
}
break;
default:
echo "Invalid request";
exit;
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_POST['submit']))
{
$new_password = $_POST['new_password'] ?? '';
$re_password = $_POST['re_password'] ?? '';
$errors = [];
// Validation
if (empty($new_password) || empty($re_password)) {
$errors[] = "Both fields are required.";
} elseif ($new_password !== $re_password) {
$errors[] = "Passwords do not match.";
} elseif (!preg_match('/[A-Za-z]/', $new_password)) {
$errors[] = "Password must contain at least one letter.";
} elseif (!preg_match('/\d/', $new_password)) {
$errors[] = "Password must contain at least one digit.";
} elseif (!preg_match('/[!@#$%^&*(),.?":{}|<>]/', $new_password)) {
$errors[] = "Password must contain at least one special character.";
}
// If no errors, update the password
if (empty($errors)) {
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = $course;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare the statement based on type
switch ($type) {
case 'student':
$stmt = $conn->prepare("UPDATE student_info SET password = ? WHERE id = ?");
$stmt->bind_param("si", $new_password, $id);
break;
case 'admin':
// echo "admin password changed<br>";
// echo "$usernameadmin";
$stmt = $conn->prepare("UPDATE admin_info SET password = ? WHERE username = ?");
$stmt->bind_param("ss", $new_password, $usernameadmin);
break;
case 'examiner':
$stmt = $conn->prepare("UPDATE examiner SET password = ? WHERE examiner_id = ?");
$stmt->bind_param("si", $new_password, $id);
break;
default:
echo "Invalid request";
exit;
}
// Execute the statement
if ($stmt->execute()) {
echo "<script>
alert('Password changed successfully!');
</script>";
} else {
echo "<p style='color: red;'>Error updating password. Please try again.</p>";
}
$stmt->close();
$conn->close();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Password</title>
<link rel="stylesheet" href="change_password.css">
</head>
<body>
<a href="javascript:history.back()" class="back-button">←</a>
<h1>Change Password</h1>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="post" action="">
<label for="new_password">Enter New Password:</label>
<input type="password" name="new_password" id="new_password" required>
<label for="re_password">Re-enter Password:</label>
<input type="password" name="re_password" id="re_password" required>
<button type="submit" name="submit">Change Password</button>
</form>
</body>
</html>