connect_error) { die("Connection failed: " . $conn->connect_error); } if (!empty($_POST['email']) && !empty($_POST['password'])) { $email = mysqli_real_escape_string($conn, $_POST['email']); $password = mysqli_real_escape_string($conn, $_POST['password']); // Check user in registration table $userCheck = mysqli_query($conn, "SELECT * FROM registration WHERE email='$email' AND password='$password'"); if ($userCheck && mysqli_num_rows($userCheck) > 0) { $_SESSION['loggedInUser'] = $email; $_SESSION['show_message'] = 'Logged In Successfully'; // Check if user exists in streak table $streakCheck = mysqli_query($conn, "SELECT * FROM user_streak WHERE email='$email'"); $currentDate = date("Y-m-d"); if (mysqli_num_rows($streakCheck) > 0) { // User exists, update streak $streakRow = mysqli_fetch_assoc($streakCheck); $lastLoginDate = $streakRow['login_date']; $streak = $streakRow['streak']; // Check if the user logged in on consecutive days $lastLoginTimestamp = strtotime($lastLoginDate); $currentTimestamp = strtotime($currentDate); $dateDiff = ($currentTimestamp - $lastLoginTimestamp) / (60 * 60 * 24); // Difference in days if ($dateDiff == 1) { // User logged in consecutive days, increment streak $newStreak = $streak + 1; } elseif ($dateDiff > 1) { // User missed a day, reset streak $newStreak = 1; } else { // User logged in today again, no change to streak $newStreak = $streak; } // Update streak and last login date $updateStreak = "UPDATE user_streak SET streak='$newStreak', login_date='$currentDate' WHERE email='$email'"; if (mysqli_query($conn, $updateStreak)) { echo "Streak updated!"; } else { echo "Error updating streak: " . mysqli_error($conn); } } else { // User doesn't exist, insert new streak record $insertStreak = "INSERT INTO user_streak (email, streak, login_date) VALUES ('$email', 1, '$currentDate')"; if (mysqli_query($conn, $insertStreak)) { echo "New streak started!"; } else { echo "Error inserting streak: " . mysqli_error($conn); } } // Redirect to homepage after successful login header('Location: homepage.php'); } else { $_SESSION['show_message'] = 'Invalid Email or Password'; } } else { $_SESSION['show_message'] = 'All fields are required'; } } ?>