From 45a7eb94fd5598a83d5aec79daf42216fffd8d40 Mon Sep 17 00:00:00 2001
From: totoW <anthony.weiss1910@gmail.com>
Date: Thu, 11 Jan 2024 15:28:05 +0100
Subject: [PATCH] User activity and the counter auto-updating is done with this
 commit

---
 html/data_treatment/AJAX/online_users.php     | 23 ++++++++++++++++++
 html/data_treatment/update_activity.php       | 20 ++++++++++++++++
 startbootstrap-agency-gh-pages/index.php      | 24 +++++++++++++++++++
 .../shoppingPage.php                          |  8 +++++++
 4 files changed, 75 insertions(+)
 create mode 100644 html/data_treatment/AJAX/online_users.php
 create mode 100644 html/data_treatment/update_activity.php

diff --git a/html/data_treatment/AJAX/online_users.php b/html/data_treatment/AJAX/online_users.php
new file mode 100644
index 0000000..d76ffa5
--- /dev/null
+++ b/html/data_treatment/AJAX/online_users.php
@@ -0,0 +1,23 @@
+<?php
+// online_users.php - Get count of online users with cleanup
+
+include '../connect_database.php';
+
+// Clean up old sessions before retrieving online user count
+$timeout = 300; // 5 minutes
+$cleanup_sql = "DELETE FROM user_sessions WHERE last_activity < (NOW() - INTERVAL $timeout SECOND)";
+$conn->query($cleanup_sql);
+
+// Retrieve online user count
+$sql = "SELECT COUNT(*) AS online_users FROM user_sessions";
+$result = $conn->query($sql);
+
+if ($result->num_rows > 0) {
+    $row = $result->fetch_assoc();
+    echo $row['online_users'];
+} else {
+    echo "0";
+}
+
+$conn->close();
+?>
\ No newline at end of file
diff --git a/html/data_treatment/update_activity.php b/html/data_treatment/update_activity.php
new file mode 100644
index 0000000..97d1f4e
--- /dev/null
+++ b/html/data_treatment/update_activity.php
@@ -0,0 +1,20 @@
+<?php
+// update_activity.php - Update user's last activity timestamp with cleanup
+
+include 'connect_database.php';
+
+if (isset($_SESSION['username'])) {
+    $username = $_SESSION['username'];
+
+    // Update user's last activity timestamp
+    $sql = "INSERT INTO user_sessions (username) VALUES ($username)
+            ON DUPLICATE KEY UPDATE last_activity = CURRENT_TIMESTAMP";
+
+    $conn->query($sql);
+
+    // Clean up old sessions (e.g., sessions older than 5 minutes)
+    $timeout = 300; // 5 minutes
+    $cleanup_sql = "DELETE FROM user_sessions WHERE last_activity < (NOW() - INTERVAL $timeout SECOND)";
+    $conn->query($cleanup_sql);
+}
+?>
\ No newline at end of file
diff --git a/startbootstrap-agency-gh-pages/index.php b/startbootstrap-agency-gh-pages/index.php
index 3b56a43..4c935cc 100644
--- a/startbootstrap-agency-gh-pages/index.php
+++ b/startbootstrap-agency-gh-pages/index.php
@@ -1,5 +1,6 @@
 <!DOCTYPE html>
 <?php session_start();
+include("../html/data_treatment/update_activity.php");
 if(!isset($_SESSION["username"])){
     header("location: ../html/login.php");
     exit;
@@ -56,6 +57,8 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl
         <!-- Font Awesome icons (free version)-->
         <script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
         <script src="https://kit.fontawesome.com/883fc94f2e.js" crossorigin="anonymous"></script>
+        <!-- JQuery -->
+        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
         <!-- Google fonts-->
         <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css" />
         <link href="https://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700" rel="stylesheet" type="text/css" />
@@ -80,6 +83,11 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl
                         }
                     </style>
                     <ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
+                        
+                    <li>
+                        <div id="onlineUsers">Loading ...</div>
+                        </li>
+                        
                         <li>
                         <form action="/search" method="GET">
                             <input type="text" name="query" id=search placeholder="Search...">
@@ -332,5 +340,21 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl
         <!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *-->
         <script src="https://cdn.startbootstrap.com/sb-forms-latest.js"></script>
         <script src="js/slideBox.js"></script>
+        <script>
+            function updateOnlineUsers() {
+            $.ajax({
+                url: '../html/data_treatment/AJAX/online_Users.php',
+                success: function (data) {
+                    $('#onlineUsers').text('Online Users: ' + data);
+                }
+            });
+        }
+
+        // Update online user count every 30 seconds
+        setInterval(updateOnlineUsers, 30000);
+
+        // Initial update
+        updateOnlineUsers();
+        </script>
     </body>
 </html>
diff --git a/startbootstrap-agency-gh-pages/shoppingPage.php b/startbootstrap-agency-gh-pages/shoppingPage.php
index f1ece7f..17045cf 100644
--- a/startbootstrap-agency-gh-pages/shoppingPage.php
+++ b/startbootstrap-agency-gh-pages/shoppingPage.php
@@ -1,3 +1,11 @@
+<?php 
+session_start();
+include("../html/data_treatment/update_activity.php");
+if(!isset($_SESSION["username"])){
+    header("location: ../html/login.php");
+    exit;
+} 
+?>
 <!DOCTYPE html>
 
 <html lang="en">
-- 
GitLab