94 lines
4.0 KiB
PHP
94 lines
4.0 KiB
PHP
<?php
|
|
$page_title = 'Change Password - Bookmarkly';
|
|
|
|
// Add profile-page class to body for CSS targeting
|
|
$GLOBALS['body_class'] = 'profile-page';
|
|
|
|
require_once 'include/auth.php'; // Ensures user is logged in
|
|
|
|
// Flash messages for feedback
|
|
$message = '';
|
|
$message_type = '';
|
|
if (isset($_SESSION['flash_message'])) {
|
|
$message = $_SESSION['flash_message']['text'];
|
|
$message_type = $_SESSION['flash_message']['type'];
|
|
unset($_SESSION['flash_message']);
|
|
}
|
|
|
|
// Handle POST request for changing password
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$current_password = $_POST['current_password'] ?? '';
|
|
$new_password = $_POST['new_password'] ?? '';
|
|
$confirm_password = $_POST['confirm_password'] ?? '';
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
// Basic validation
|
|
if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
|
|
$_SESSION['flash_message'] = ['text' => 'Please fill in all fields.', 'type' => 'warning'];
|
|
} elseif ($new_password !== $confirm_password) {
|
|
$_SESSION['flash_message'] = ['text' => 'New passwords do not match.', 'type' => 'warning'];
|
|
} else {
|
|
// Fetch user's current hashed password
|
|
$stmt = $pdo->prepare("SELECT password FROM users WHERE id = ?");
|
|
$stmt->execute([$user_id]);
|
|
$user = $stmt->fetch();
|
|
|
|
// Verify current password
|
|
if ($user && password_verify($current_password, $user['password'])) {
|
|
// Hash the new password and update it
|
|
$new_hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
|
|
$update_stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
|
|
if ($update_stmt->execute([$new_hashed_password, $user_id])) {
|
|
$_SESSION['flash_message'] = ['text' => 'Password changed successfully.', 'type' => 'success'];
|
|
} else {
|
|
$_SESSION['flash_message'] = ['text' => 'Error changing password.', 'type' => 'danger'];
|
|
}
|
|
} else {
|
|
$_SESSION['flash_message'] = ['text' => 'Incorrect current password.', 'type' => 'danger'];
|
|
}
|
|
}
|
|
|
|
header('Location: profile.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'include/header.php';
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-6">
|
|
<h1>Change Password</h1>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
|
|
<?php echo htmlspecialchars($message); ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<form action="profile.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="current_password" class="form-label">Current Password</label>
|
|
<input type="password" class="form-control" id="current_password" name="current_password" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="new_password" class="form-label">New Password</label>
|
|
<input type="password" class="form-control" id="new_password" name="new_password" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="confirm_password" class="form-label">Confirm New Password</label>
|
|
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Password</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'include/footer.php'; ?>
|