Forum Moderators: coopster
<?php
// read the text file
$filename = "counter.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo 'This page has been viewed '.$contents.' times' ;
// do the calculations
$contents++;
// write to the text file
$somecontent = $contents;
if (!$handle2 = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle2, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle2);
?>
There are a couple of problems with it. Firstly it goes 1, 2, 13, 1214 etc etc
Secondly I obviously want to overwrite the counter value, not add it at the end of the text file.
If anyone can point my tired brain in the right direction it would be much appreciated.
Helen.
check this thread out for how to write to file:
[webmasterworld.com...]
that is slightly different from what you want, but you can see how to use the fopen more cleanly than what you are doing.
What you want to do is open it for reading first, copy the content to a variable, close it, open it for writing second, then write the new value to the file.
There is also something that looks like it might be an error:
if (!$handle2 = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
is this supposed to be:
if (!( $handle2 = fopen($filename, 'a') ) ) {
echo "Cannot open file ($filename)";
exit;
}
that is, it is not the case that this was successful
also this looks wrong:
if (fwrite($handle2, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
you want the echo to be in an else condition, otherwise the test will always print out success:
if (fwrite($handle2, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
else
{
echo "Success, wrote ($somecontent) to file ($filename)";
}
<?php
#config
$path_to_logs="";
$whoami=preg_replace("/[\/\\\:]/","_",$_SERVER["SCRIPT_FILENAME"]);
if (!file_exists($path_to_logs.$whoami.".count")) touch($path_to_logs.$whoami.".count");
$handle=fopen($path_to_logs.$whoami.".count","r");
$text=fread($handle,100)*1;
fclose($handle);
echo "Page has been seen ".($text)." times";
$handle=fopen($path_to_logs.$whoami.".count","w");
fwrite($handle,++$text);
fclose($handle);
?>
you'll need to set the $path_to_logs if you don't want the record files in the same directory as the page it refers to.
this script is also designed so it can work as an include() - whatever file it is included from, it will reflect the count of that file.
The code was giving me a headache too, I must confess I got it from a tutorial without fully understanding it.
vincevincevince your code seems to work great. Forgive me if I'm being dumb, but where does it write the page count?
it writes them to files based upon the path to the script, changing / \ and : to _ with .count file extension
for example, if the code is include() from, or put into a page residing at:
/home/user/httpdocs/mypage.php
then the file is called:
_home_user_httpdocs_mypage.php.count
that way it is quite unlikely to confuse 2 pages, or to overwrite your existing content
it will be in the same directory as the script is called from, unless you set the $path_to_logs (line 3) to something else. if the file doesn't already exist, it will make it.