You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Code2C/profile.php

88 lines
2.2 KiB

6 months ago
<?php
session_start();
// Check if the user is logged in
if (!isset($_SESSION["loggedInUser"])) {
header("Location: login.php");
exit();
}
$conn = new mysqli('localhost', 'root', '', 'user'); // Your database credentials
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$email = trim($_SESSION["loggedInUser"]);
// Fetch the user details from the registration table
$sql = "SELECT * FROM registration WHERE email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
// Get user details
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
$user_id = $user['id'];
} else {
echo "No user found.";
exit();
}
// Get the streak
$streak = 0;
$today = date('Y-m-d');
$last_login = null;
// Get the login dates of the user
$sql = "SELECT login_date FROM user_streaks WHERE user_id = ? ORDER BY login_date DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$streak_result = $stmt->get_result();
// Calculate the streak
while ($row = $streak_result->fetch_assoc()) {
if (!$last_login) {
$last_login = $row['login_date'];
$streak++;
} else {
$diff = (strtotime($last_login) - strtotime($row['login_date'])) / (60 * 60 * 24); // Difference in days
if ($diff == 1) {
$streak++;
$last_login = $row['login_date'];
} else if ($diff > 1) {
break; // Streak breaks if the difference is more than 1 day
}
}
}
// Display the profile page
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile</title>
</head>
<body>
<h1>User Profile</h1>
<p><strong>Email:</strong> <?php echo htmlspecialchars($user['email']); ?></p>
<p><strong>Name:</strong> <?php echo htmlspecialchars($user['name']); ?></p>
<p><strong>Country:</strong> <?php echo htmlspecialchars($user['country']); ?></p>
<p><strong>Date of Birth:</strong> <?php echo htmlspecialchars($user['dob']); ?></p>
<p><strong>Streak:</strong> <?php echo $streak; ?> days</p>
</body>
</html>
<?php
$stmt->close();
$conn->close();
?>