-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
176 lines (153 loc) · 4.15 KB
/
Copy pathprofile.php
File metadata and controls
176 lines (153 loc) · 4.15 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
<?php
include('DB.php');
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Update Profile</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
font-family: 'Inter', sans-serif;
background-color: #f5f7fa;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
}
.profile-form {
background: #fff;
padding: 2.5rem;
border-radius: 16px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
max-width: 450px;
width: 100%;
}
.profile-form h2 {
margin-bottom: 1.5rem;
color: #333;
text-align: center;
}
.form-group {
margin-bottom: 1.2rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #555;
font-weight: 600;
}
input {
width: 100%;
padding: 0.75rem;
border: 1px solid #ccc;
border-radius: 8px;
font-size: 1rem;
outline: none;
transition: border-color 0.3s;
}
input:focus {
border-color: #3498db;
}
.btn-submit {
width: 100%;
padding: 0.9rem;
background-color: #3498db;
color: white;
border: none;
border-radius: 10px;
font-weight: bold;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.3s;
}
.btn-submit:hover {
background-color: #2980b9;
}
.back-link {
display: block;
margin-top: 1rem;
text-align: center;
color: #3498db;
text-decoration: none;
font-weight: 500;
}
.back-link:hover {
text-decoration: underline;
}
@media (max-width: 480px) {
.profile-form {
padding: 2rem 1.5rem;
}
}
</style>
</head>
<body>
<h1>Welcome, <?php
if (isset($_SESSION['user_id'])) {
echo $_SESSION['user'] . "<br>";
} else {
header("Location: login.php");
exit();
}
if (isset($_SESSION['welcome_time']) && time() - $_SESSION['welcome_time'] <= 60) {
echo "We're happy you joined us!";
} ?></h1>
<form class="profile-form" method="post">
<h2>Update Your Profile</h2>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="user" placeholder="Enter new username" value="<?= htmlspecialchars($_SESSION['user']) ?>" required>
</div>
<div class="form-group">
<label for="email">Email (must be unique)</label>
<input type="email" name="email" id="email" placeholder="Enter new email" value="<?= htmlspecialchars($_SESSION['email']) ?>" required>
</div>
<div class="form-group">
<label for="password">New Password</label>
<input type="password" name="pass" id="password" placeholder="Leave blank to keep old password">
</div>
<div class="form-group">
<label for="confirm">Confirm Password</label>
<input type="password" name="confirm_pass" id="confirm" placeholder="Leave blank to keep old password">
</div>
<button type="submit" class="btn-submit" name="submit">Save Changes</button>
<a href="index.php" class="back-link">← Back to Dashboard</a>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['user'];
$email = $_POST['email'];
$password = $_POST['pass'];
$confirm_pass = $_POST['confirm_pass'];
if ($password != $confirm_pass) {
echo "Passwords do not match!";
} else {
if (!empty($password)) {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$sql = "UPDATE users SET user='$username', email='$email', password='$hashed_password' WHERE id=" . $_SESSION['user_id'];
} else {
$sql = "UPDATE users SET user='$username', email='$email' WHERE id=" . $_SESSION['user_id'];
}
if (mysqli_query($conn, $sql)) {
echo "<script>alert('Profile updated successfully!');</script>";
//update session variables
$_SESSION['user'] = $username;
$_SESSION['email'] = $email;
header("Location: index.php");
exit();
} else {
echo "Error updating profile: " . mysqli_error($conn);
}
}
}
// Close the database connection
mysqli_close($conn);
?>
</body>
</html>