Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
AJAX User Activity
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods Completed

2020-07-09 07:50:57
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]


<?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>
activity2.png
activity2.png
2020-07-16 06:18:01
Added: binded parameter for excluding admin, user or multiple user ID's from activity results (optional).
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 9
  • Members: 0
  • Guests online: 9
  • Comments: 3
  • Messages: 1,728