homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Need a little guidance with a script

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

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)

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


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

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

echo $hits;



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

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)

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



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

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved