-2

//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);
}

3 Answers3

3

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.

Community
  • 1
  • 1
felixgaal
  • 2,403
  • 15
  • 24
1

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.

raPHPid
  • 567
  • 2
  • 10
0

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.

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • how to run a php script continously when the system is on without usng cron, because i need to set the refreshing interval dynamicaly??? can anyone suggest a method.. – user1431987 Jun 11 '12 at 08:45
  • I did in the second part of my answer. Should I elaborate on it? What don't you understand exactly? – sachleen Jun 11 '12 at 14:00