Honesty Rocks! truth rules.

Mimic Cronjobs... Is there a way?Please help

HOME      >>       Programming

ewcreators

So, my free cpanel account (not on this server, another one) doesn't support cronjob among its awesome features.

Now i had read a post online for alternative to cron jobs, that :

Now depending on the actual functionality of the script (if its just a php page that needs to be executed). You could create a simple database that holds the last time executed, and a script included on your website that checks to see the last time it was executed and if its longer then X amount of time, execute it again. (use some ajax or something to do it behind the scenes)

Now its not the first time ive heard this. A popular, unique online web based game i play (legacy-game.net) uses this method of cron jobs.
Can anyone post a script or help for this? Is there any online script for this sort of thing?

And if you could , could you please list a proper step-by-step way to mimic cron jobs.
I sort of understand how to do it, but i dont get the part on how to manipulate time..etc. I mean to check the time with php with the time in the mysql database.

[hr=noshade] [/hr]
Nevermind , i got it. Easier then i thought!
Anyways, I'm gonna guide all the newbs and people who might need help in the future with this :

This script is for running on an hourly basis. How it is to be run, that is upto you. You can use ajax or anything. I have put it on all of my pages(basically ive put it in one page which is required in all pages) and once that page is visited, the page is run.

<?phpecho "<Br /> CRONJOB DETAILS : <br />";//create a variable which contains the current hour of the server(from 1 - 12)$timetocheck=date(g);//select the database table which contains the cronjob details$select=mysql_query("SELECT * FROM cron");$time=mysql_fetch_array($select);//setting the variable $timetoadd to the data of $timetocheck$timetoadd=$timetocheck;//echo "Last time executed : $time[last_executed]";//checking if the current time (in hour from 1-12) is not equal to the time given in the database of which the last cron was executed//if it is not, then it will initiate the required cron jobs along with updating the value for last_executed time in the database.if ($timetoadd != $time[last_executed]) {$q=mysql_query("UPDATE cron SET last_executed='$timetocheck'");require('../crons/turns.php');echo "<br/>The Following Crons have been run :</br>";echo "-Turns Update Cron (+200/h)<br />";}else {echo "<br />No cron is currently being run.<br />";}?>


galexcd

If I understand you correctly, something like this should work.

function execute(){//place your code here that you would normally put into the php page for the cronjob.}$time=5; //how often do you want this to execute (in minutes)$lasttime=mysql_result($result,0,"time"); //set this to the last time a page was loaded from the server//--------------------------------------------------------------------------------------------------$ctime=time();$time*=60;while($ctime-$time>=$lasttime){execute();ctime-=$time;}$time=time()-($ctime-$lasttime);//--------------------------------------------------------------------------------------------------mysql_query("UPDATE `name` SET `time` = ".$time);//you're sql query to add the last time loaded back into the server.

Reply if you have any other problems :lol:

ewcreators

while thats good, ill stick to mine XDThanks though, im keeping that as a template just incase i have any problems.Helpful as always alex :P:lol:


galexcd

The only problem I see with your code is that if more time goes by than the cron is set for, it will only execute once. Mine will execute as many times as it would have if it was on a cron job. Both are good scripts, it just depends on what kind of script you want executed, and if its essential to happen the correct number of times.