09 Cached Active Users On Homepage

Started by Mindless, July 21, 2012, 10:50:03 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mindless

In index.php above this or where you want :

Code (php) Select
$HTMLOUT .= sprintf("<p><font class='small'>{$lang['foot_disclaimer']}</font></p>", $TBDEV['site_name']);


Add this :
Code (php) Select
//==09 users on index
$active3 ="";
$file = "./cache/active.txt";
$expire = 30; // 30 seconds
if (file_exists($file) && filemtime($file) > (time() - $expire)) {
$active3 = unserialize(file_get_contents($file));
} else {
$dt = sqlesc(time() - 180);
$active1 = mysql_query("SELECT id, username, class, warned, donor FROM users WHERE last_access >= $dt ORDER BY class DESC") or sqlerr(__FILE__, __LINE__);
    while ($active2 = mysql_fetch_assoc($active1)) {
        $active3[] = $active2;
    }
    $OUTPUT = serialize($active3);
    $fp = fopen($file, "w");
    fputs($fp, $OUTPUT);
    fclose($fp);
} // end else
$activeusers = "";
if (is_array($active3))
foreach ($active3 as $arr) {
    if ($activeusers) $activeusers .= ",\n";
    $activeusers .= "<span style=\"white-space: nowrap;\">";
    $arr["username"] = "<font color='#" . get_user_class_color($arr['class']) . "'> " . htmlspecialchars($arr['username']) . "</font>";
    $donator = $arr["donor"] === "yes";
    $warned = $arr["warned"] === "yes";

    if ($CURUSER)
        $activeusers .= "<a href='{$TBDEV['baseurl']}/userdetails.php?id={$arr["id"]}'><b>{$arr["username"]}</b></a>";
    else
        $activeusers .= "<b>{$arr["username"]}</b>";
    if ($donator)
         $activeusers .= "<img src='{$TBDEV['pic_base_url']}star.gif' alt='Donated' />";
    if ($warned)
        $activeusers .= "<img src='{$TBDEV['pic_base_url']}warned.gif' alt='Warned' />";
    $activeusers .= "</span>";
}

if (!$activeusers)
    $activeusers = "{$lang['index_noactive']}";
   
    $HTMLOUT .= "<div style='text-align:left;width:80%;border:1px solid blue;padding:5px;'>
    <div style='background:lightgrey;height:25px;'><span style='font-weight:bold;font-size:12pt;'>{$lang['index_active']}</span></div><br />";
  $HTMLOUT .="<table border='1' cellpadding='10' cellspacing='0' width='100%'>
<tr class='table'>
<td class='text'>{$activeusers}</td>";
  $HTMLOUT .="</tr></table></div>";



And if you dont have it add this to include/user_functions.php
Code (php) Select
function get_user_class_color($class)
{
    switch ($class)
    {       

        case UC_USER: return "8E35EF";
        case UC_POWER_USER: return "f9a200";
        case UC_VIP: return "009F00";
        case UC_UPLOADER: return "0000FF";
        case UC_MODERATOR: return "FE2E2E";
        case UC_ADMINISTRATOR: return "B000B0";
        case UC_SYSOP: return "4080B0";
    }
    return "";
}


In lang/en/lang_index.php add
Code (php) Select
'index_active' => "Active users",
'index_noactive' => "Sorry - No users active presently",


Make sure your cache folder is writeable - Any errors, improvements ect please post them :)