diff --git a/html/data_treatment/AJAX/online_users.php b/html/data_treatment/AJAX/online_users.php new file mode 100644 index 0000000000000000000000000000000000000000..d76ffa588fb647bdd954a421aba4c8faba412208 --- /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 0000000000000000000000000000000000000000..97d1f4e9f53817b14d922da32b316c34391b054a --- /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 3b56a433eb7a9d2fad790934e03952283785f189..4c935cc31ac5650581a75bb1ef82a8193948a56a 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 f1ece7f6f2c20eb66e604e7502834f25e8a367d2..17045cf9ab3c3f08d8273d533c33258ecb653ba9 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">