Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mikseri.net profile page visitor counter
/* == PAGECOUNTER == */
/* == Pitää lukua päivittäisistä vierailijamääristä == */
$pagecounter_cache_key = 'pagecounter_' . $user_id . '_' . date('Y-m-d');
$pagecounter = $cache->get($pagecounter_cache_key);
if (!is_array($pagecounter))
{
$pagecounter = array();
$res = $db->query("SELECT date, count from pagecounter WHERE WEEK(date) = WEEK(CURDATE()) AND user_id = ?", array($user_id));
if ($res->numRows() > 0)
{
foreach($res as $item)
{
$pagecounter[$item['date']] = $item['count'];
}
}
if(!isset($pagecounter[date('Y-m-d')]))
{
$db->query("INSERT INTO pagecounter SET date = curdate(), user_id = ?", array($user_id));
$pagecounter[date('Y-m-d')] = 0;
}
}
if ($user_id != $myid or rand(1, 10) == 1)
{
$pagecounter[date('Y-m-d')]++;
$db->query("UPDATE pagecounter SET count = ? WHERE date = CURDATE() AND user_id = ? LIMIT 1", array($pagecounter[date('Y-m-d')], $user_id));
}
$cache->set($pagecounter_cache_key, $pagecounter, 3600);
$smarty->assign('pagecounter', $pagecounter);
/*
mysql> describe pagecounter;
+---------+------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+------------+-------+
| date | date | NO | MUL | 0000-00-00 | |
| user_id | int(10) unsigned | NO | | 0 | |
| count | int(10) unsigned | NO | MUL | 0 | |
+---------+------------------+------+-----+------------+-------+
3 rows in set (0.01 sec)
*/
{if $userdata.id == $thisuser.id}
<br />
Profiilia katsottu tällä viikolla {$pagecounter|@array_sum} <span class="light">(näkyy vain itselle)</span>
{/if}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment