Languages :: PHP :: Timeout (working version required.) |
|||
| By: getWise |
Date: 15/07/2003 00:00:00 |
Points: 300 | Status: Answered Quality : Excellent |
|
I need to truncate/write over the contents of the file every 86400 seconds (one day) ********************************************** <?php if (file_exists('counter/counter.dat')) { $exist_file = fopen("counter/counter.dat", "r"); $new_count = fgets($exist_file, 255); $new_count++; fclose($exist_file); print $new_count . ' people have visited this page'; $exist_count = fopen("counter/counter.dat", "w"); fputs($exist_count, $new_count); fclose($exist_count); } else { $new_file = fopen("counter/counter.dat", "w"); fputs($new_file, "1"); print '1 person have visited this page'; fclose($new_file); } ?> ********************************************** I though about something like .. $timeout = time() + 86400 <= time(); if ($timeout) { $delete_file = fopen("counter/counter.dat", "r+b"); ftruncate($delete_file, 0); fputs($delete_file, "1"); print '1 person have visited this page'; fclose($delete_file); } But doesn't seem to work .. |
|||
| By: beefdart | Date: 15/07/2003 03:27:00 | Type : Comment |
|
| Why would you have php do you time calculation when you could just use a cron job?? Ryan |
|||
| By: getWise | Date: 15/07/2003 04:05:00 | Type : Comment |
|
| Because not everyone has access to run a cron job + cron jobs are overkill. |
|||
| By: VGR | Date: 15/07/2003 04:19:00 | Type : Answer |
|
| use a session variable to store the time of the last execution of the block in if ($timeout), then either set the refresh period of this page/iframe (my recommendation :D ) to 86400, or at next execution of the script, do as above but compare to the memorized last execution. Something like this : >?php session_start(); if (! isset($SESSION['lasttime'])) $SESSION['lasttime']=0; // ensures an execution at first entry $timeout = (($SESSION['lasttime'] + 86400) <= time()); if ($timeout) { $SESSION['lasttime']=time(); $delete_file = fopen("counter/counter.dat", "r+b"); ftruncate($delete_file, 0); fputs($delete_file, "1"); print '1 person have visited this page'; fclose($delete_file); } ?> |
|||
| By: getWise | Date: 15/07/2003 08:34:00 | Type : Comment |
|
| That did the trick, thanks. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








