visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Timeout (working version required.)


By: getWise U.S.A.  Date: 15/07/2003 00:00:00  English  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 English  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 English  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 English  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 English  Type : Comment
That did the trick, thanks.

Do register to be able to answer

EContact
browser fav
page generated in 455.549000 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page