Hello,
I have a problem with big traffic counter. From time to time filesystem cut the file with data to zero size. Could you help me with it?
Here's the code:
I have a problem with big traffic counter. From time to time filesystem cut the file with data to zero size. Could you help me with it?
Here's the code:
Code:
<?
error_reporting(0);
define('Data', './count.cnt');
chdir(dirname(__FILE__));
//php 4
if (file_exists(Data))
$File = fopen(Data, 'r+');
else
$File = fopen(Data, 'w+');
if ($File !== false) {
do {
flock($File, LOCK_EX, $Lock);
// sleep(1);
} while ($Lock);
$Content = fread($File, filesize(Data));
rewind($File);
$Hosts = unserialize(base64_decode($Content));
//php 5 only
// $Hosts = unserialize((file_get_contents(Data)));
// $Hosts = unserialize(base64_decode(file_get_contents(Data)));
if (isset($_GET['Id'])) {
$Hosts[$_GET['Id']][1]++;
//php 4
fwrite($File, base64_encode(serialize($Hosts)));
flock($File, LOCK_UN);
fclose($File);
//php 5 only
// file_put_contents(Data, (serialize($Hosts)));
// file_put_contents(Data, base64_encode(serialize($Hosts)));
header("Location: ".$_GET['Out']);
exit();
}
else {
$Url = parse_url($_SERVER['HTTP_REFERER']);
$Host = strtolower(str_replace('[URL unfurl="true"]www.',[/URL] '', $Url['host']));
if ($Host != '')
$Hosts[$Host][0]++;
else
$Hosts['!unknown'][0]++;
//php 4
fwrite($File, base64_encode(serialize($Hosts)));
flock($File, LOCK_UN);
fclose($File);
//php 5 only
// file_put_contents(Data, (serialize($Hosts)));
// file_put_contents(Data, base64_encode(serialize($Hosts)));
}
}
?>