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

if (!$sock = fsockopen("www.google.com", 80, $errno, $errstr,20))
    {
?>
<script type="text/javascript">

alert("Connection problem");

</script>

<?php       
    }

// retriving data from database and checking the site is down

else
    {
    for($i=0;$i<count($res);$i++)
        {
        $fp=fsockopen($res[$i]['url'],80,$errno,$errstr,30);
        if($fp==false)
            {
 ?>
 <script type="text/javascript">

 alert("Website is down");

 </script>

 <?php

// writing data to database and sending mail

mail($email[0]['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);
}

Answers


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 it to stay in the form of a web page, automatically reloading itself, then look at the JavaScript function setTimeout.


There are a few ways to do this. First, you can use cron jobs to automatically run a script at a set time. Your javascript won't work as the PHP script will be running directly on the server and you won't be able to show anything in a browser.

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.


Need Your Help

Swift: method overloads that only differ in return type

ios swift swift2

I keep seeing Swift classes where two methods are defined that only differ in return type. I'm not used to working in languages where this is allowed (Java, C#, etc), so I went looking for the

Switch case on enum type

ios swift enums switch-statement

I have an issue where I wish to do a switch-case on an enum type (SearchFilterOptionType), but for some reason Swift/the Xcode compiler does not recognise the member.