Hi,
I've been working on a stats-script for some time now and I'm experiencing some problems. The stats-script stores the data in .dat-files that are created by the php-script it self (when needed). I tried this on my own computer (Apache-server) and it work just like I planned it. When I uploaded the script to my internet server something strange happens: the .dat-files are created correctly, sometimes the data is written to it correctly and some-times not at all. Furthermore the .dat files cannot be deleted or edited via WS-ftp. When I try to view the stats the script prompts that 9.dat (the file for september) cannot be read. I don't understand what is going wrong. I hope someone would be kind enough to 'dig' through my code to find a solution! I've posted the code of my script below.
Thanx in advance Michiel
Code:
addstat.php
<?php
/* Browser-info */
if (eregi("MSIE ([0-9]+\.[0-9]+)", $HTTP_USER_AGENT, $regs))
{
$name = "Internet Explorer";
$version = $regs[1];
}
elseif (eregi("Opera ([0-9]+\.[0-9]+)", $HTTP_USER_AGENT, $regs))
{
$name = "Opera";
$version = $regs[1];
}
elseif (eregi("Mozilla/([0-9]+\.[0-9]+)", $HTTP_USER_AGENT, $regs))
{
$name = "Netscape";
$version = $regs[1];
}
else {
$name = "Overig";
$version = "";
}
$browser = $name." ".$version;
/* OS-info */
if (eregi("(win|windows)[ ]*((nt)*[ /]*([0-9]+(\.?[0-9]+))*)", $HTTP_USER_AGENT, $regs))
$os = "Windows $regs[2]";
elseif (stristr($HTTP_USER_AGENT, "Linux"))
$os = "Linux";
elseif (stristr($HTTP_USER_AGENT, "Unix"))
$os = "Unix";
elseif (stristr($HTTP_USER_AGENT, "Mac"))
$os = "Macintosh";
else $os = "Overig";
// Register user's operating system
AddStat(os, $os, $thisyear);
// Register user's browser
AddStat(browser, $browser, $thisyear);
// Register user's screenresolution
AddStat(screenresolution, $ss, $thisyear);
// Register user's screencolors
AddStat(screencolors, $sc, $thisyear);
// Register user's referrer
AddStat(referrers, $ref, $thisyear);
// Register visitor
AddVisit($thishour, $thisday);
// Finaly update cookies
AddCookies();
/* Functions */
function AddStat($table, $new_value, $unique_visit) {
$dir = date("Y");
if (!is_dir($dir)) mkdir("$dir", 0777);
if (file_exists("$dir/$table.dat")) $fp=file("$dir/$table.dat");
else $fp=Array();
$match=false;
foreach ($fp as $line)
{
list($value,$hits,$unique)=explode('|',trim($line));
$stat[$value]=Array('hits'=>$hits,'unique'=>$unique);
if ($value==$new_value) $match=true;
}
if ($match)
{
$stat[$new_value]['hits']++;
if (!$unique_visit == 1) $stat[$new_value]['unique']++;
}
else $stat[$new_value]=Array('hits'=>1,'unique'=>1);
if (!$fp = fopen("$dir/$table.dat", "w")) die("Couldn't open $dir/$table.dat for writing!");
foreach ($stat as $key=>$value)
{
$line="$key|{$value['hits']}|{$value['unique']}\r\n";
fputs($fp,$line,strlen($line));
}
fclose($fp);
}
function AddVisit($unique_visit_hour, $unique_visit_day) {
$dir = date("Y");
$table = date("n");
$new_day = date("j");
$new_hour = date("H");
if (!is_dir($dir)) mkdir("$dir", 0775);
if (file_exists("$dir/$table.dat")) $fp=file("$dir/$table.dat");
else $fp=Array();
$match=false;
foreach ($fp as $line)
{
list($day,$hour,$hits,$unique_hour,$unique_day)=explode('|',trim($line));
$stat[$day][$hour]= Array('hits'=>$hits,'unique_hour'=>$unique_hour,'unique_day'=>$unique_day);
if ($stat[$new_day][$new_hour]) $match=true;
}
if ($match)
{
$stat[$new_day][$new_hour]['hits']++;
if (!$unique_visit_hour == 1) $stat[$new_day][$new_hour]['unique_hour']++;
if (!$unique_visit_day == 1) $stat[$new_day][$new_hour]['unique_day']++;
}
else
{
if (!$unique_visit_day == 1)
{
$stat[$new_day][$new_hour]= Array('hits'=>1,'unique_hour'=>1,'unique_day'=>1);
}
else
{
$stat[$new_day][$new_hour]= Array('hits'=>1,'unique_hour'=>1,'unique_day'=>0);
}
}
if (!$fp = fopen("$dir/$table.dat", "w")) die("Couldn't open $dir/$table.dat for writing!");
foreach ($stat as $key=>$day)
{
$key_day = $key;
foreach ($day as $key=>$hour)
{
$line ="$key_day|$key|{$hour['hits']}|{$hour['unique_hour']}|{$hour['unique_day']}\r\n";
fputs($fp,$line,strlen($line));
}
}
fclose($fp);
}
function AddCookies() {
$now = time();
if (!$thishour)
{
$next_hour = mktime(date("G")+1, 0, 0, date("n"), date("d"), date("Y"));
$sec_to_next_hour = $next_hour-$now;
setcookie('thishour', '1', $sec_to_next_hour);
}
if (!$thisday)
{
$next_day = mktime(0, 0, 0, date("n"), date("d")+1, date("Y"));
$sec_to_next_day = $next_day-$now;
setcookie('thisday', '1', $sec_to_next_day);
}
if (!$thisyear)
{
$next_year = mktime(0, 0, 0, 1, 1, date("Y")+1);
$sec_to_next_year = $next_year-$now;
setcookie('thisyear', '1', $sec_to_next_year);
}
}
?>
Part II in the next message