Welcome to WebmasterWorld Guest from 54.159.165.175

Forum Moderators: coopster & jatar k

Need a little guidance with a script

   
6:51 pm on Jul 24, 2008 (gmt 0)

5+ Year Member



I'm trying to develop a script that will increment by one integer each time the script is called. So basically - if someone clicks the button/form that the script is attached to; it will increment that variable by one integer. The difficult part I'm having is saving that variable total each night at midnight - and then wiping the variable clean; so it starts at zero for the next day. I'd like to save that previous number too. Any thoughts?
7:14 pm on Jul 24, 2008 (gmt 0)

5+ Year Member



This is what I've got so far. For some reason it won't increase the number in hits.txt by one. Any thoughts? (I'll work on saving/clearing the data nightly later.)

--------------------

<?php
$hits = file_get_contents("hits.txt");
$hits = $hits + 1;

$handle = fopen("hits.txt", "w");
fwrite($handle, $hits);
fclose($handle);

echo $hits;

?>

10:25 pm on Jul 24, 2008 (gmt 0)

5+ Year Member



That looks ok to me. Are the any errors? If you're using Linux/BSD you'll probably need to chmod hits.txt to 666.

If that doesn't help put error_reporting(E_ALL); at the top of the script to make sure there are no other errors.

You could also try typecasting $hits (see below), but that shouldn't be the issue in PHP.

$hits = (int)file_get_contents("hits.txt");

2:37 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Shouldn`t you need to clear the original file before writing the new one?

dc

3:03 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



If you had an earlier bug that wrote something invalid to your file, then adding 1 to that invalid value will just produce another invalid value.
Try deleting the file and letting your script create it afresh.
As far as the midnight roll up is concerned, if it is important that it be done right at midnight then you will need a cron job.
Otherwise, you could add a comma and a timestamp to your file and check the timestamp every time you increment the counter. If the date has changed by one day, then you report the old number somewhere and start again from 1.
cheers
j
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month