Alternative to cron

  • tautvys92
  • Newbie
  • Newbie
  • tautvys92
  • Posts: 5

Post 3+ Months Ago

I need to execute MySQL query (or just run PHP file) every minute. But I have no cron on my server...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

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. ?>
  • tautvys92
  • Newbie
  • Newbie
  • tautvys92
  • Posts: 5

Post 3+ Months Ago

Thank you. I'll try it. But will this work when there will be no users online? And all sesions will be ended?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

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();)
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

The above code will not work if there are no users on the site.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

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:

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 53 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.