Jump to content

[SOLVED] A sort of cron jobs.


John_S

Recommended Posts

Hello everybody,I am trying to write a function which would work similarly to a cron job, each time a visitor will come to my site, the cron function will be called and will check if there is a cronjob to execute. I want to store all the cronjobs within a flat file so there won't be too many connections to the database. However when I test my function it refuses to work, it doesn't output any errors at all, but there is no result displayed neither the flat file database is changed.

Could somebody please tell me what's wrong? Here's my code:

function cron($name) {
        $file = file('./crons/crons.db.php');
        foreach ($file as $line) {
             if(ereg("$name", $line)) {                   $cronjob = explode("|", $line);                   if (time() >= $cronjob[2]) {                          include("./crons/$name.cron.php");                   }             }         } // End of Foreach
        $source = fopen('./crons/crons.db.php', w);
        foreach ($file as $line) {                 if(!ereg("$name", $line)) {                     fwrite($source, $line);                 } else {                      $lastrun = time();                     $toberun = time() + (24 * 60 * 60);                    fwrite($source, "|$name|$lastrun|$toberun|$status| "); }      }   } // End of Fucntion

In my files, I call the function like this: cron("db_backup"); [db_backup = databse backup.]

Thanks a lot in advance!Yours John_S

Link to comment
https://forums.phpfreaks.com/topic/152680-solved-a-sort-of-cron-jobs/
Share on other sites

<?php

function cron($name) {
$file = file('./crons/crons.db.php');

foreach ($file as $line) {
     if(ereg("$name", $line)) {
	 	$cronjob = explode("|", $line);
		if (time() >= $cronjob[2]) {
			include("./crons/$name.cron.php");
		}
	}
}//End of Foreach

$source = fopen('./crons/crons.db.php', w);

foreach ($file as $line) {          
	if(!ereg("$name", $line)) {                     
		fwrite($source, $line);                 
	} else {
		$lastrun = time();
		$toberun = time() + (24 * 60 * 60);
		fwrite($source, "|$name|$lastrun|$toberun|$status| ");
	}

}
}//End of Fucntion

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.