Post by Jacob on Jun 12, 2009 14:30:41 GMT -5
This script shows how many people are on your site, without using MySQL.
The first thing to do is to create the array with data about the current visitor.
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.
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.
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.
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.
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:
The first thing to do is to create the array with data about the current visitor.
$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.
$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.
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.
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.
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:
<?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));
?>