Skip to content
Snippets Groups Projects
Commit 45a7eb94 authored by totoW's avatar totoW
Browse files

User activity and the counter auto-updating is done with this commit

parent 15867f6f
No related branches found
No related tags found
No related merge requests found
<?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
<?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
<!DOCTYPE html> <!DOCTYPE html>
<?php session_start(); <?php session_start();
include("../html/data_treatment/update_activity.php");
if(!isset($_SESSION["username"])){ if(!isset($_SESSION["username"])){
header("location: ../html/login.php"); header("location: ../html/login.php");
exit; exit;
...@@ -56,6 +57,8 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl ...@@ -56,6 +57,8 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl
<!-- Font Awesome icons (free version)--> <!-- Font Awesome icons (free version)-->
<script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script> <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> <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--> <!-- 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=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" /> <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 ...@@ -80,6 +83,11 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl
} }
</style> </style>
<ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0"> <ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
<li>
<div id="onlineUsers">Loading ...</div>
</li>
<li> <li>
<form action="/search" method="GET"> <form action="/search" method="GET">
<input type="text" name="query" id=search placeholder="Search..."> <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 ...@@ -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="https://cdn.startbootstrap.com/sb-forms-latest.js"></script>
<script src="js/slideBox.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> </body>
</html> </html>
<?php
session_start();
include("../html/data_treatment/update_activity.php");
if(!isset($_SESSION["username"])){
header("location: ../html/login.php");
exit;
}
?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment