Results 1 to 3 of 3

Thread: Need a small help

  1. #1
    Join Date
    Dec 2007
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Need a small help

    i want to execute a script for every 10 seconds . and the loop is 10 minutes long.
    I written the code like this but i got timeout warning. i have changed the execution time too in .ini file. please tell me what is wrong or give me a sample code for this:

    $time = time();
    $timeafter10mins = $time+10*60;
    $result = mysql_query("select * from tbl_outbox where UniqueID like '%12345%'");
    while($time < $timeafter10mins)
    {
    while($row = mysql_fetch_array($result))
    {
    $uniqueid = $row['UniqueID'];
    $text = $row['message'];
    $from = $row['From_Mobile_number'];
    $dated = $row['dated'];
    }
    if($text!="" && stristr($text,$uniqueid))
    {
    $data = "<?xml version='1.0' ?>
    <SMS NO='$from'><SMStext>".$text."</SMStext><SMStime>".$dated."</SMStime></SMS>";
    $fh = fopen($uniqueid.'.xml', 'w');
    fwrite($fh, $data);
    $xmlfile = $uniqueid.'.xml'
    ?>
    <script>window.location.href='<?=$xmlfile?>';</script><?
    exit();
    }
    $time = $time + 10;
    sleep($time);

    And also i wanted to delete the xml file after 10 mins.
    Please give me a solution. Waiting for your response,
    Thanks in advance,
    Sridevi.

  2. #2
    Join Date
    Jul 2008
    Location
    Johannesburg, South Africa
    Posts
    31
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default

    You do not need to do a loop to force a script to rerun in a browser. Just use the header function in your PHP...

    header ("Refresh: 600; url=" . $_SERVER['PHP_SELF']);

    That line will force the page to refresh in the browser every 600 seconds. This will not work on command-line/cgi mode of PHP. If you are trying to get your script to process every 10 minutes from command line rather look at running a cron job than trying to get the script to time its own execution.

    The time limitation is there to stop a PHP script running forever (i.e. normally as part of an infinite loop) as PHP scripts aren't meant to run continuosly as a back ground process, but run once, do a task, end.

    Hope this info helps

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You can even consider using AJAX if you don't want to reload the page completely especially if you are dealing with big complex pages. So you can add a timer using JavaScript (setInterval) and invoke the AJAX calling function, which will communicate with the server. The server then will execute the PHP code and return the results. You can pass any number of parameters to the server-side if you wish

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •