Running a Script Regularly PHP

Both UNIX and Windows NT have facilities for running programs according to a schedule. In UNIX you can edit your crontab file, and in Windows you use the scheduling service. These are useful when you wish to perform some maintenance function as part of your PHP-powered site. You may write a script to download the list of files at the Slashdot. Another script might rebuild the index for a local search engine. Both crontab and the scheduling service take a command line and execute it at a given time. If you're not familiar with the details, either read the main page for crontab, or type at /? in a Windows command shell.

You have two choices for invoking a PHP script from the command line. If you have PHP as a stand-alone executable, you can call it and use the path to the script for the only argument. This is probably the way to go on a Windows machine, because the installation provides php.exe. A UNIX installation likely will be compiled as an Apache module, and no stand-alone executable will be available. In this case, you can use another program to make an HTTP connection. The text-only browser, Lynx is well suited for this purpose.

Remember, the Web server executes PHP scripts. Executing scripts from the root user's crontab will allow them greater ability to do damage. It's probably best to execute the script from the Web server's crontab. Using Lynx to run the script avoids this issue but raises another. Unless you put the script in a protected directory, anyone will be able to run it. Simply protect the script with a username and password. Lynx will allow you to specify these on the command line.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics