i want to run the following php script continously in specified intervals of time and can be set by a user using a form. how to do this?
//i need to run this script repetadly and the time interval can be set by a user
// checking the internet connection
// retriving data from database and checking the site is down
// writing data to database and sending mail
mail($email['email_id'],"website is down",$res[$i]['url']." is down"); $inputs=array('website_id'=>$res[$i['website_id'], 'date'=>date("Y-m-d H:i:s"),'reason'=>"website down"); $obj3->addLog($inputs); } } } sleep(300); }
Your problem with cron is this "and the time interval can be set by a user".
You can use cron such way, that you run script every 30 seconds or every N seconds. That is your smallest granularity for running script.
Now, if user sets that script must be run every 30 minutes, first thing you do at script is check that, if script needs to proceed - if last run was before that 30 minutes that user set. Script pseudo goes like this:
1) load last run from DB, compare it with interval - do we need to proceed, if not exit 2) do what script needs to do 3) save to the DB last run time
Of course this all is only necessary, if user needs to be able to dynamically change run interval and dont want to touch cron.
Look at the cron service. Someone has already explained it here.
If you want the user to have the page open while it's doing all of this so the script can report data on the screen, I would just set the time in the URL as GET parameters mypage.php?refreshTime=1800 and then read it using $_GET['refreshTime'] and then use the following line to set the refresh time.
<meta http-equiv="refresh" content="$refreshTime">
This will cause the page to reload after $refreshTime seconds. Your PHP to do whatever it is it needs to do will run on each refresh as it does now when you load the page.