<?
// counter.php
$f = "data.txt";
$fp = file($f);
$i=0;
$new = $fp[$i] +1;
$newdata = fopen("$f", "w");
fputs($newdata,"$new");
echo "$new";
?>
Originally posted by kabatak
replace data.txt with the file name in which you want to store the counter's data. place them both in the same directory.
PHP:<? // counter.php $f = "data.txt"; $fp = file($f); $i=0; $new = $fp[$i] +1; $newdata = fopen("$f", "w"); fputs($newdata,"$new"); echo "$new"; ?>
oh yes, put fclose($newdata); so that will be.Originally posted by NONO
hi, doesn't it need to fclose() upon fputs($newdata,"$new") ?
thanks!
<?
$f = "data.txt";
$fp = file($f);
$i=0;
$new = $fp[$i] +1;
$newdata = fopen("$f", "w");
fputs($newdata,"$new");
fclose($newdata);
echo "$new";
?>
understand. if no database, are there others way to do that? I'd like to know too.Originally posted by tandoc
don't use this for high traffic sites! it crashes the system or doesn't count properly and gets fux0rd
<?php
function count_hit()
{
$file_pointer= fopen("hits.txt", "r+");
if ($file_pointer == false)
{
return "Error: could not open the file! It may not exist!";
exit;
}
$hits= fread($file_pointer, filesize("hits.txt"));
$hits= trim($hits);
$hits++;
fseek($file_pointer, 0);
$result= fwrite($file_pointer, $hits);
if ($result == false)
{
return "Error: could not write to the file!";
exit;
}
else {
return $hits;
}
$close= fclose($file_pointer);
if ($close == false)
{
echo "Error: could not close the file!";
exit;
}
}
?>