midnite 1 Posted April 28, 2010 Report Share Posted April 28, 2010 (edited) Hi there,Is there any way that PHP alone can perform periodic tasks?There are a few solutions, with the help of the third parties:1) using JavaScript setTimeout() or setInterval() on a webpage to periodically request the PHP script. But we have to keep the web browser open. If the browser is closed, everything stops.2) using the OS scheduler, or another language to periodically call that PHP script. This will make the program become less portable. And also, if using another language to call the PHP, why not having the application written in that language.JS runs on web browser. PHP runs on web server. But PHP relies on browser calls to be triggered. My question seems like asking if PHP can escape from browser calls, run on its own on the web server. It is still alright if the PHP script has to be triggered by a browser call. But is it possible to keep the PHP script running after just one browser call (and close the browser)?Thank you very much,midnite Edited April 30, 2010 by midnite Quote Link to post Share on other sites
Synook 47 Posted April 28, 2010 Report Share Posted April 28, 2010 It is very common to use the Unix task scheduler (cron) to schedule PHP scripts to run. PHP is non-threaded and has an inbuilt timeout, and so is not suitable for running ongoing tasks. Remember that the PHP interpreter relies on the underlying platform to get itself run anyway, its sole job being to take a script, written in PHP, and interpret it. I mean, it can't even serve web pages by itself!You can run PHP scripts on the server arbitrarily from the terminal. Quote Link to post Share on other sites
midnite 1 Posted April 28, 2010 Author Report Share Posted April 28, 2010 Hi Synook,Thanks very much for the reply and thanks for reminding me that PHP has an inbuilt timeout. So it is almost impossible to have periodic task on its own. Using cron on Linux is a good idea. A bit off topic. In your opinion, is it that Java seems to be the best choice, compared to C and PHP, if I has to access HTML, analyse DOM, and do it periodically? But outputting webpages from Java is definitely not as handy as PHP does.Best Regards,midnite Quote Link to post Share on other sites
Synook 47 Posted April 28, 2010 Report Share Posted April 28, 2010 I don't really see much difference - PHP has more HTML-related functions and libraries though. What were you looking for in Java? Quote Link to post Share on other sites
midnite 1 Posted April 28, 2010 Author Report Share Posted April 28, 2010 Java itself has task scheduler and it supports threads. I am more familiar with PHP and feel that PHP is stronger in manipulating HTML documents too. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.