-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
140 lines (123 loc) · 6.89 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
<?php
require_once 'config.php';
require_once 'functions.php';
if (!is_logged_in()) {
redirect('login.php');
}
require('navbar.php');
$user_id = $_SESSION['user_id'];
$error = '';
$success = '';
// Fetch user information
$query = "SELECT username, email FROM users WHERE id = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$user = mysqli_fetch_assoc($result);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = sanitize_input($_POST['username']);
$email = sanitize_input($_POST['email']);
$new_password = sanitize_input($_POST['new_password']);
if (empty($username) || empty($email)) {
$error = "Username and email are required.";
} elseif (!empty($new_password) && strlen($new_password) < 6) {
$error = "New password must be at least 6 characters long.";
} else {
$query = "UPDATE users SET username = ?, email = ? WHERE id = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "ssi", $username, $email, $user_id);
if (mysqli_stmt_execute($stmt)) {
$_SESSION['username'] = $username;
$success = "Profile updated successfully.";
if (!empty($new_password)) {
$hashed_password = hash_password($new_password);
$query = "UPDATE users SET password = ? WHERE id = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "si", $hashed_password, $user_id);
mysqli_stmt_execute($stmt);
$success .= " Password updated.";
}
} else {
$error = "Failed to update profile. Please try again.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en" class="h-full bg-gray-100">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile - Online To-Do List</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
</head>
<body class="h-full">
<div class="min-h-full">
<header class="bg-white shadow">
<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
<h1 class="text-3xl font-bold tracking-tight text-gray-900">User Profile</h1>
</div>
</header>
<main>
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
<div class="px-4 py-6 sm:px-0">
<div class="bg-white overflow-hidden shadow rounded-lg">
<div class="px-4 py-5 sm:p-6">
<?php if ($error): ?>
<div class="rounded-md bg-red-50 p-4 mb-4">
<div class="flex">
<div class="flex-shrink-0">
<i class="fas fa-exclamation-circle text-red-400"></i>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-red-800"><?php echo $error; ?></h3>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="rounded-md bg-green-50 p-4 mb-4">
<div class="flex">
<div class="flex-shrink-0">
<i class="fas fa-check-circle text-green-400"></i>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-green-800"><?php echo $success; ?></h3>
</div>
</div>
</div>
<?php endif; ?>
<a href="dashboard.php" class="text-blue-600 hover:underline">← Back to Dashboard</a>
<form method="POST" action="" class="space-y-6">
<div>
<label for="username" class="block text-sm font-medium leading-6 text-gray-900">Username</label>
<div class="mt-2">
<input type="text" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
</div>
</div>
<div>
<label for="email" class="block text-sm font-medium leading-6 text-gray-900">Email</label>
<div class="mt-2">
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
</div>
</div>
<div>
<label for="new_password" class="block text-sm font-medium leading-6 text-gray-900">New Password (leave blank to keep current password, must be at least 6 characters)</label>
<div class="mt-2">
<input type="password" id="new_password" name="new_password" minlength="6" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
</div>
</div>
<div>
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Update Profile</button>
</div>
</form>
</div>
</div>
</div>
</div>
</main>
</div>
</body>
</html>