Hi,
I've got a statistics-script that registers (unique) visits, os, screenresolution, screencolor etc. The data is stored in flat-file .dat files. The script itself works alright, I only got a problem with .dat files that are created by the script.
When I run the script on my localhost everything works fine. When I run the script on my webserver the .dat files are created correctly, but the aren't writable (though they are chmodded) and the can't be edited/ deleted via an ftp program.
I really don't know what has gone wrong. I hope someone can help me out, 'cos it's getting quite frustrating :mad:
Here is my script (addstat.php):
// 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);
}
}
Thanx in advance for your replies!
Michiel