It is currently Tue Feb 09, 2010 4:20 am




Post new topic Reply to topic  [ 1 post ] 
 [Tutorial] Flatfile Users Online 
Author Message
Site Admin
User avatar

Joined: Sat Jan 03, 2009 2:49 pm
Posts: 24
Post [Tutorial] Flatfile Users Online
The first thing to do is to create the array with data about the current visitor.

Code:
$ip = $_SERVER['REMOTE_ADDR'];
$visitor = array(
'page' => $page,
'time' => time()
);


The variable $page should be a unique name for the current page ($_SERVER['PHP_SELF'] should work). Next, get the data from the users online file, and add (or update) the data about the current visitor.

Code:
$online = unserialize(file_get_contents('data/online.php'));
$online[$ip] = $visitor;


The next piece of code deletes all vistors from the file that have not been updated in 10 minutes (so they are probably not on your site anymore). The number 900, the number of seconds, can be changed to suit your site.

Code:
foreach($online as $k => $v)
{
if($online[$k]['time'] < time() - 900)
{
unset($online[$k]);
}
}


Now we have our finished array of visitors on the site. All that's left to do is count how many or on each page, and write the new contents to the file.

Code:
foreach($online as $k => $v)
{
if(!isset($onlinecount[$online[$k]['page']]))
$onlinecount[$online[$k]['page']] = 0;
$onlinecount[$online[$k]['page']] += 1;
}
file_put_contents('data/online.php', serialize($online));


You can get the number of visitors on a page by accessing $onlinecount['name of page']. The next code piece will get and print the numbers online in the whole site.

Code:
foreach($count as $k => $v)
{
$total += $count[$k];
}
echo $total;


Save the code in a .php file, and include it in all pages that you want to track. The first time you go to your site, you will get a file not found warning, because the online.php file has not yet been created. Reload the page, and the file will be created, so the error will no longer appear.
Here is the complete code:

Code:
<?php
//remember to set $page
$ip = $_SERVER['REMOTE_ADDR'];
$visitor = array(
'page' => $page,
'time' => time()
);
$online = unserialize(file_get_contents('data/online.php'));
$online[$ip] = $visitor;
foreach($online as $k => $v)
{
if($online[$k]['time'] < time() - 900)
{
unset($online[$k]);
}
}
foreach($online as $k => $v)
{
if(!isset($onlinecount[$online[$k]['page']]))
$onlinecount[$online[$k]['page']] = 0;
$onlinecount[$online[$k]['page']] += 1;
}
file_put_contents('data/online.php', serialize($online));
?>


Fri Apr 03, 2009 3:15 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.