Project_Bank / profile.php
Kshitij2604's picture
Upload 30 files
102fe5c verified
<?php
// Include database connection and authentication check
require_once 'includes/db_connect.php';
require_once 'includes/auth_check.php';
// Redirect to login if not logged in
requireLogin();
$error = '';
$success = '';
$user = null;
// Get user information
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM users WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
} else {
$error = "User not found.";
}
// Process profile update form
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) {
$email = $conn->real_escape_string(trim($_POST['email']));
// Validate email
if (empty($email)) {
$error = "Email is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Please enter a valid email address.";
} else {
// Check if email is already used by another user
$check_query = "SELECT id FROM users WHERE email = ? AND id != ?";
$check_stmt = $conn->prepare($check_query);
$check_stmt->bind_param("si", $email, $user_id);
$check_stmt->execute();
$check_result = $check_stmt->get_result();
if ($check_result->num_rows > 0) {
$error = "Email is already in use by another account.";
} else {
// Update user profile
$update_query = "UPDATE users SET email = ? WHERE id = ?";
$update_stmt = $conn->prepare($update_query);
$update_stmt->bind_param("si", $email, $user_id);
if ($update_stmt->execute()) {
$_SESSION['email'] = $email;
$success = "Profile updated successfully.";
// Refresh user data
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
} else {
$error = "Failed to update profile: " . $conn->error;
}
}
}
}
// Process password change form
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password'])) {
$current_password = trim($_POST['current_password']);
$new_password = trim($_POST['new_password']);
$confirm_password = trim($_POST['confirm_password']);
// Validate inputs
if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
$error = "All password fields are required.";
} elseif (strlen($new_password) < 6) {
$error = "New password must be at least 6 characters long.";
} elseif ($new_password !== $confirm_password) {
$error = "New passwords do not match.";
} elseif (!password_verify($current_password, $user['password'])) {
$error = "Current password is incorrect.";
} else {
// Update password
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$update_query = "UPDATE users SET password = ? WHERE id = ?";
$update_stmt = $conn->prepare($update_query);
$update_stmt->bind_param("si", $hashed_password, $user_id);
if ($update_stmt->execute()) {
$success = "Password changed successfully.";
} else {
$error = "Failed to change password: " . $conn->error;
}
}
}
// Include header
include 'includes/header.php';
?>
<div class="row mb-4">
<div class="col-md-12">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.php">Home</a></li>
<li class="breadcrumb-item active">My Profile</li>
</ol>
</nav>
<h2><i class="fas fa-id-card me-2"></i> My Profile</h2>
<p class="lead">Manage your account settings and password</p>
</div>
</div>
<?php if (!empty($error)): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (!empty($success)): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<i class="fas fa-check-circle me-2"></i> <?php echo $success; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="row">
<div class="col-md-4 mb-4">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h5 class="mb-0"><i class="fas fa-user me-2"></i> Account Information</h5>
</div>
<div class="card-body">
<div class="text-center mb-4">
<div class="avatar-circle mb-3">
<span class="avatar-text"><?php echo strtoupper(substr($user['username'], 0, 1)); ?></span>
</div>
<h5><?php echo htmlspecialchars($user['username']); ?></h5>
<span class="badge <?php echo $user['user_type'] === 'faculty' ? 'bg-danger' : 'bg-success'; ?> mb-2">
<?php echo ucfirst($user['user_type']); ?>
</span>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<strong><i class="fas fa-envelope me-2"></i> Email:</strong>
<span class="float-end"><?php echo htmlspecialchars($user['email']); ?></span>
</li>
<li class="list-group-item">
<strong><i class="fas fa-clock me-2"></i> Joined:</strong>
<span class="float-end"><?php echo date('M d, Y', strtotime($user['created_at'])); ?></span>
</li>
<li class="list-group-item">
<strong><i class="fas fa-sign-in-alt me-2"></i> Last Login:</strong>
<span class="float-end">
<?php echo $user['last_login'] ? date('M d, Y H:i', strtotime($user['last_login'])) : 'Never'; ?>
</span>
</li>
<li class="list-group-item">
<strong><i class="fas fa-toggle-on me-2"></i> Status:</strong>
<span class="float-end">
<span class="badge <?php echo $user['status'] === 'active' ? 'bg-success' : 'bg-secondary'; ?>">
<?php echo ucfirst($user['status']); ?>
</span>
</span>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card shadow-sm mb-4">
<div class="card-header bg-primary text-white">
<h5 class="mb-0"><i class="fas fa-edit me-2"></i> Edit Profile</h5>
</div>
<div class="card-body">
<form method="POST" action="profile.php" class="needs-validation" novalidate>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" value="<?php echo htmlspecialchars($user['username']); ?>" readonly>
<div class="form-text">Username cannot be changed.</div>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
<div class="invalid-feedback">
Please enter a valid email address.
</div>
</div>
<div class="mb-3">
<label for="user_type" class="form-label">Account Type</label>
<input type="text" class="form-control" id="user_type" value="<?php echo ucfirst($user['user_type']); ?>" readonly>
<div class="form-text">Account type cannot be changed.</div>
</div>
<button type="submit" name="update_profile" class="btn btn-primary">
<i class="fas fa-save me-2"></i> Update Profile
</button>
</form>
</div>
</div>
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h5 class="mb-0"><i class="fas fa-key me-2"></i> Change Password</h5>
</div>
<div class="card-body">
<form method="POST" action="profile.php" class="needs-validation" novalidate>
<div class="mb-3">
<label for="current_password" class="form-label">Current Password</label>
<div class="input-group">
<input type="password" class="form-control" id="current_password" name="current_password" required>
<button class="btn btn-outline-secondary toggle-password" type="button" data-target="#current_password">
<i class="fas fa-eye"></i>
</button>
</div>
<div class="invalid-feedback">
Please enter your current password.
</div>
</div>
<div class="mb-3">
<label for="new_password" class="form-label">New Password</label>
<div class="input-group">
<input type="password" class="form-control" id="new_password" name="new_password" required minlength="6">
<button class="btn btn-outline-secondary toggle-password" type="button" data-target="#new_password">
<i class="fas fa-eye"></i>
</button>
</div>
<div class="form-text">Password must be at least 6 characters long.</div>
<div class="invalid-feedback">
Please enter a new password (minimum 6 characters).
</div>
</div>
<div class="mb-3">
<label for="confirm_password" class="form-label">Confirm New Password</label>
<div class="input-group">
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
<button class="btn btn-outline-secondary toggle-password" type="button" data-target="#confirm_password">
<i class="fas fa-eye"></i>
</button>
</div>
<div class="invalid-feedback">
Please confirm your new password.
</div>
</div>
<button type="submit" name="change_password" class="btn btn-warning">
<i class="fas fa-key me-2"></i> Change Password
</button>
</form>
</div>
</div>
</div>
</div>
<style>
.avatar-circle {
width: 100px;
height: 100px;
background-color: #007bff;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.avatar-text {
font-size: 48px;
color: white;
font-weight: bold;
}
</style>
<?php
// Include footer
include 'includes/footer.php';
// Close connection
$conn->close();
?>