1
kugai
Garbage collection/user log
  • 2004/8/9 18:55

  • kugai

  • Just popping in

  • Posts: 1

  • Since: 2004/8/9 1


Im not sure if anyone has done something like this already. I couldn't find anything. I hacked together this little script to do the garbage collection(removing old online users) and log who is online.

You will need to make a directory(preferably outside your www document path for security), upload the file there, change the settings(below) and schedule a cron job to run every so often.

Settings:
1) Change the second line /PATH/TO/XOOPS to your XOOPS folder. This must be a full or relative path since it is outside the main XOOPS folder.

2) Change the $savepath to where the user log should be saved. Probably want to use the same folder you place the script in.

3) Change the 300 in $online_handler->gc(300) line to how many second a user must be idle before removed. (optional)

Then add to your cron job list to run how every often you want the update. ie:
*/5 * * * * php /PATH/TO/SCRIPT/cleaner.php > /dev/null
to run it every five minutes

You can then comment of these lines from modules/system/blocks/system_blocks.php because the new script will handle all the garbage collection.
Quote:

// if (mt_rand(1, 100) < 11) {
// $online_handler->gc(300);
// }


The log will be named users_DATE.txt
Format will be:
TIME, NUMBER OF USERS
USERNAME,IP,MODULE

Obviously you could cut out parts to make it only do the garbage collection, only the logging, put the log in a database or other format, etc. I just wanted to put it out there so someone could tell me if this is messed up or make it better.

Script:

Quote:

// cleaner.php
<?php

include "/PATH/TO/XOOPS/mainfile.php";
include_once XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/misc.php';

// Set to your script path
$savepath = "/PATH/TO/SAVELOG";

$date = date("mdy");
$time = date("H:i:s");

$filename = "$savepath/users_$date.txt";
$fp = fopen($filename, "a");

fputs($fp, $time.",");

$start = 0;
$online_handler =& xoops_gethandler('online');
// Do garbage collection
$online_handler->gc(300);
$online_total =& $online_handler->getCount();
// Limit number of users returned
$limit = ($online_total > 200) ? 200 : $online_total;
$criteria = new CriteriaCompo();
$criteria->setLimit($limit);
$criteria->setStart($start);
$onlines =& $online_handler->getAll($criteria);
$count = count($onlines);
$module_handler =& xoops_gethandler('module');
$modules =& $module_handler->getList(new Criteria('isactive', 1));

fputs($fp, $count."\n");

for ($i = 0; $i < $count; $i++) {
if ($onlines[$i]['online_uid'] == 0) {
$onlineUsers[$i]['user'] = '';
} else {
$onlineUsers[$i]['user'] =& new XoopsUser($onlines[$i]['online_uid']);
}
$onlineUsers[$i]['ip'] = $onlines[$i]['online_ip'];
$onlineUsers[$i]['updated'] = $onlines[$i]['online_updated'];
$onlineUsers[$i]['module'] = ($onlines[$i]['online_module'] > 0) ? $modules[$onlines[$i]['online_module']] : '';
}

for ($i = 0; $i < $count; $i++) {
if (is_object($onlineUsers[$i]['user'])) {
$lineout=$onlineUsers[$i]['user']->getVar('uname').",";
} else {
$lineout=$xoopsConfig['anonymous'].",";
}
$lineout.=$onlineUsers[$i]['ip'].",".$onlineUsers[$i]['module']."\n";
fputs($fp, $lineout);
}

fclose($fp);

?>

Login

Who's Online

153 user(s) are online (72 user(s) are browsing Support Forums)


Members: 0


Guests: 153


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits