Forum Moderators: coopster

Message Too Old, No Replies

php counter - why does 13 come after 2?

this is probably just friday night stupid syndrome

         

HelenDev

5:08 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am trying to put together a little counter for a page...

<?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.

Glacai

5:36 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



I think just changing the second fopen to 'w' will fix it.

if (!$handle2 = fopen($filename, 'w'))

ie. 1, 2, 12+1 = 13 etc...

isitreal

5:38 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The first thing is that you are using the second fopen for a, not w, a is append, which is what is happening.

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)";
}

vincevincevince

6:26 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, your code was giving me a headache, try this one:


<?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.

HelenDev

8:37 am on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for all your replies. Changing the second fopen to 'w' did sort out the problem.

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?

vincevincevince

4:28 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi,

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.

HelenDev

7:50 am on Mar 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, I can see the file now.

Cheers vince.