Alternative to cron

  • tautvys92
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jan 11, 2009
  • Posts: 5
  • Status: Offline

Post May 20th, 2009, 8:04 am

I need to execute MySQL query (or just run PHP file) every minute. But I have no cron on my server...
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 20th, 2009, 8:04 am

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 21st, 2009, 8:38 pm

PHP Code: [ Select ]
<?php
// $interval needs to be in seconds
function cronjob($interval, $ses_name = 'T_CRONJOB')
{
    if(isset($_SESSION[$ses_name]))
    {
        $time = time() - $_SESSION[$ses_name];
        if($time > $interval)
        {
            return true;
        }
    }
   
    $_SESSION[$ses_name] = time();
    return false;
}
 
if(cronjob(60) == true)
{
    // do your code here
}
?>
  1. <?php
  2. // $interval needs to be in seconds
  3. function cronjob($interval, $ses_name = 'T_CRONJOB')
  4. {
  5.     if(isset($_SESSION[$ses_name]))
  6.     {
  7.         $time = time() - $_SESSION[$ses_name];
  8.         if($time > $interval)
  9.         {
  10.             return true;
  11.         }
  12.     }
  13.    
  14.     $_SESSION[$ses_name] = time();
  15.     return false;
  16. }
  17.  
  18. if(cronjob(60) == true)
  19. {
  20.     // do your code here
  21. }
  22. ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • tautvys92
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jan 11, 2009
  • Posts: 5
  • Status: Offline

Post May 21st, 2009, 11:17 pm

Thank you. I'll try it. But will this work when there will be no users online? And all sesions will be ended?
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 21st, 2009, 11:24 pm

tautvys92 wrote:
Thank you. I'll try it. But will this work when there will be no users online? And all sesions will be ended?

Yes... this uses it's own session, which' name you could change (2 Param in cronjob();)
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post May 21st, 2009, 11:24 pm

The above code will not work if there are no users on the site.
#define NULL (::rand() % 2)
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 21st, 2009, 11:25 pm

SpooF wrote:
The above code will not work if there are no users on the site.

You're right... I don't know what I was thinking :scratchhead:
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: webspace and 129 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.