Welcome to WebmasterWorld Guest from 54.162.19.123

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Need a little guidance with a script

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

Preferred Member

10+ Year Member

joined:Apr 25, 2006
posts:409
votes: 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 July 24, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 25, 2006
posts:409
votes: 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.)

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

<?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 July 24, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 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 July 25, 2008 (gmt 0)

Senior Member

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

joined:Mar 30, 2003
posts:3719
votes: 0


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

dc

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

Junior Member

10+ Year Member

joined:Dec 18, 2004
posts:53
votes: 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.
cheers
j
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members