-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetting.php
More file actions
227 lines (200 loc) · 6.13 KB
/
setting.php
File metadata and controls
227 lines (200 loc) · 6.13 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
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
<?php
include 'src/db.php';
session_start();
if (!isset($_SESSION['email']) || !isset($_SESSION['id'])) {
header("Location: register");
exit();
}
$user_id = $_SESSION['id'];
// Fetch user
$stmt = $conn->prepare("SELECT USER_NAME, EMAIL, PROFILE_IMG, PASSWORD FROM users WHERE id=?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$msg = "";
//============= PROFILE UPDATE ===================#
if (isset($_POST['update_profile'])) {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$old_image = $user['PROFILE_IMG'];
$new_image = $old_image;
// Image Upload --------------------------------#
if (!empty($_FILES['profile_img']['name'])) {
$file = $_FILES['profile_img'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$fileSize = (int)$file['size'];
$allowed = ['jpg','jpeg','png','webp'];
if ($fileSize > 500 * 1024) {
$msg = "<div class='error'>File size must be 500KB or lower</div>";
}
else if (!in_array($ext, $allowed)) {
$msg = "<div class='error'>Only image files allowed</div>";
}
else {
$new_image = uniqid() . "." . $ext;
if (move_uploaded_file($file['tmp_name'], "uploads/users/" . $new_image)) {
if (!empty($old_image) && $old_image != 'default.png' && file_exists('uploads/users/' . $old_image)) {
unlink('uploads/users/' . $old_image);
}
} else {
$msg = "<div class='error'>Upload failed</div>";
}
}
}
// error check before DB update
if (empty($msg) || strpos($msg, 'error') === false) {
$stmt = $conn->prepare("UPDATE users SET USER_NAME=?, EMAIL=?, PROFILE_IMG=? WHERE id=?");
$stmt->bind_param("sssi", $name, $email, $new_image, $user_id);
if ($stmt->execute()) {
$_SESSION['email'] = $email;
$msg = "<div>Profile Updated successfully</div>";
}
$stmt->close();
header("location:setting");
}
}
// ================= PASSWORD CHANGE ========================================4
if (isset($_POST['change_password'])) {
$current = $_POST['current_password'];
$new = $_POST['new_password'];
$confirm = $_POST['confirm_password'];
if (!password_verify($current, $user['PASSWORD'])) {
$msg = "<div class='error'>Current password is incorrect</div>";
}
elseif ($new !== $confirm) {
$msg = "<div class='error'>Passwords do not match</div>";
}
elseif (strlen($new) < 6) {
$msg = "<div class='error'>Minimum 6 characters required</div>";
}
else {
$hashed = password_hash($new, PASSWORD_DEFAULT);
$stmt = $conn->prepare("UPDATE users SET PASSWORD=? WHERE id=?");
$stmt->bind_param("si", $hashed, $user_id);
if ($stmt->execute()) {
$msg = "<div class='success'>Password Updated Successfully</div>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Account Settings</title>
<?php include 'components/head.php'; ?>
<style>
@import url('assets/css/root.css');
body {
margin:0;
font-family: Arial;
background:#f4f6f9;
}
.container {
max-width:900px;
margin:40px auto;
}
.card {
background:#fff;
padding:25px;
border-radius:12px;
margin-bottom:20px;
box-shadow:0 4px 10px rgba(0,0,0,0.08);
}
.profile {
display:flex;
align-items:center;
gap:20px;
}
.profile img {
width:90px;
height:90px;
border-radius:50%;
object-fit:cover;
border:3px solid #007bff;
}
input {
width:100%;
padding:10px;
margin-top:5px;
border-radius:6px;
border:1px solid #ccc;
}
.form-group {
margin-bottom:15px;
}
button {
background:#007bff;
color:#fff;
padding:10px 18px;
border:none;
border-radius:6px;
cursor:pointer;
}
button:hover {
background:#0056b3;
}
.success {
background:#d4edda;
padding:10px;
margin-bottom:10px;
border-radius:5px;
}
.error {
background:#f8d7da;
padding:10px;
margin-bottom:10px;
border-radius:5px;
}
</style>
</head>
<body>
<?php include 'components/header.php'; ?>
<div class="container">
<?php echo $msg; ?>
<!-- PROFILE -->
<div class="card">
<div class="profile">
<img src="uploads/users/<?php echo $user['PROFILE_IMG'] ?>">
</div>
<h2>Profile Settings</h2>
<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value="<?php echo $user['USER_NAME']; ?>">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $user['EMAIL']; ?>">
</div>
<div class="form-group">
<label>Profile Image</label>
<input type="file" name="profile_img">
</div>
<button name="update_profile">Update Profile</button>
</form>
</div>
<!-- PASSWORD ----------------------------------------------------------->
<div class="card">
<h2>Change Password</h2>
<form method="POST">
<div class="form-group">
<label>Current Password</label>
<input type="password" name="current_password">
</div>
<div class="form-group">
<label>New Password</label>
<input type="password" name="new_password">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="confirm_password">
</div>
<button name="change_password">Change Password</button>
<a style="background-color:var(--secondary); padding:10px 15px; color:#fff; padding:8px 18px; border:none;border-radius:6px;cursor:pointer; " href="reset-password">Reset</a>
</form>
</div>
</div>
<?php include 'components/footer.php'; ?>
</body>
</html>