0

Hi, I have developed a SMS Application. There is a file name my.php I want to execute it after every 10 seconds so I can get new files which arrive. please guide me

4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by digital-ether
0

You can run it through the task scheduler, but I think you can only specify per 5 minutes as smallest interval.

0

Depends on the OS I think. With Win7 just type it, Vista and XP probably have them in accessoires or system tools.

0

If the page is only ever going to be accessed with localhost, you could just have the page open constantly and code it to refresh itself every 10 seconds.

0

its good suggestion to open a page but I want to learn how to do it using coding .

0

You can create a deamon to run indefinitely, and have it invoke your file every few minutes.

Simple version:

<?php

while(1) 
{
   sleep(10*60);

   include('my.php');

}

?>

It will need to be run from the command line. Example:

php deamon.php
0

It is best to use the command line. However, you can use the browser.

If using the browser, two things to remember:
1) The webserver will timeout, or browser/client will timeout.
2) The page will have to stay open

To prevent the server from ending the PHP process you can use:

set_time_limit(0);

http://www.php.net/manual/en/function.set-time-limit.php

This allows the PHP script to execute indefinitely.

To prevent the browser from timing out, and also allow you to close the page you can send the Content-Length header from the browser. This has the effect of completing the page load in the browser.

<?php
ob_start(); // place at top of script

echo str_pad('', 256); // IE needs some content

// ............... put any messages or HTML here ...............

$content_length = ob_get_length(); // length of content
header('Content-Length: ' . $content_length); // set the length of content

ob_flush(); // flush output to browser
flush();

// now you can do what you want as the browser has already loaded

// ............ run your script here ................

?>

As you can see, this is a bit hacky. If you run it in the command line, PHP automatically runs the script for ever and won't time out. You also don't need to send output for the browser, or trick the browser into completing the page load.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.