Show user activity live on site (or in admin panel page) live activity. Something I started to work on, seems to fit nice.
Create [color=rgb(226, 80, 65)]ajax_useractivity.php[/color]
Add to header.php
Add anywhere you like
[ATTACH type="full" alt="activity2.png"]5608[/ATTACH]
Change your refresh interval (seconds) according to bold text below. Don't set it below 10 seconds, less than 5 and you will be taxing your server performance. Update in header.php
}, [color=rgb(226, 80, 65)]15[/color]000);
</script>
Create [color=rgb(226, 80, 65)]ajax_useractivity.php[/color]
<?php
// AJAX User Activity - Queried by interval - July 9, 2020
try {
$dsn = 'mysql:dbname=database;host=localhost';
$user = 'username';
$password = 'password';
$stmt = new PDO($dsn, $user, $password);
$stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
die('Unable to connect to database');
}
$idlist = '1,6'; // Exclude User ID from activity by comma - 1: admin, 6: dummy - no spaces!
$stmt = $conn->prepare("SELECT id, username, avatar, page AS activity, class FROM users WHERE id NOT IN (:exclude) ORDER BY last_access DESC LIMIT 8");
$stmt->bindParam(":exclude", $idlist, PDO::PARAM_STR);
$stmt->execute();
$userData = array();
if ($stmt->rowCount() > 0) {
$usersactive = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($usersactive as $row => $data) {
$data['id'] = $row['id'];
$data['value'] = $row['username'];
$data['avatar'] = $row['avatar'];
$data['activity'] = $row['activity'];
$data['class'] = $row['class'];
array_push($userData, $usersactive);
}
}
echo json_encode($usersactive);
?>
Add to header.php
<script>
function fetchUserActivity() {
$(document).ready(function () {
$.ajax({
url: "ajax_useractivity.php",
method: "GET",
dataType: 'JSON',
success: function (data) {
var html_to_append = '';
$.each(data, function (i, item) {
html_to_append +=
'<a title="' + item.username + '" href="/user/?id=' + item.id + '"><img style="width:45px;height:45px;border-radius:50%;margin-right:10px;" src="' + item.avatar + '" /></a><div>' + item.activity + '</div>';
});
$("#user-activity").html(html_to_append);
}
});
});
}
fetchUserActivity();
setInterval(function () {
fetchUserActivity()
}, 15000);
</script>
Add anywhere you like
<!-- AJAX User Activity - 15sec interval -->
<div id="user-activity"></div>
[ATTACH type="full" alt="activity2.png"]5608[/ATTACH]
Change your refresh interval (seconds) according to bold text below. Don't set it below 10 seconds, less than 5 and you will be taxing your server performance. Update in header.php
}, [color=rgb(226, 80, 65)]15[/color]000);
</script>