If you have access a the command-line version of PHP, scheduled tasks can use a different, probably better - and faster - way of scheduling.
In this situation a script which can be run from the command-line PHP interface (PHP CLI) is used. This doesn't require the front-end in order to work; it is self-contained, and can run even if the web server is down!
In order to schedule such a task, one has to use the following command line to the host's CRON interface:
/usr/local/bin/php /home/USER/webroot/cli/cron.php
where /usr/local/bin/php is the path to the site PHP CLI executable, /home/USER/webroot is the absolute path to your web site's root, and cron.php is the name of your script located in the 'cli' subdirectory. This information can be supplied by your host provider.
Important | |
---|---|
We cannot know the path to your PHP CLI executable or the absolute path to your web site's root. This is the kind of information that only the people who have set up your server (your host) can possibly know. |
Note | |
---|---|
The use of the PHP CGI executable is not suitable. To check which version is available run the PHP command with the --v option which will display the executable specifics. Sometimes hosts will give you the path to the PHP CGI binary. This will NOT work with CLI scripts. CGI (Common Gateway Interface) and CLI (Command Line Interface) are two completely separate things. The former is designed to run scripts which will be served as web pages. The latter is designed to run command line scripts, usually for use with CRON. |
We earlier mentioned the use of a secret key to be provided when the script is run. There is also another alternative for PHP-CLI scripts. This is (if using Apache as your web server - most do) is to add some rules to your .htaccess file that prohibits anyone other than the local machine from running the script.
Note | |
---|---|
For the following rules to work, you must have an Apache web server with mod_rewrite enabled. |
For example: If the file you want to run is called 'script_name', you could add the following to your .htaccess file to ensure that nobody else can run the script:
RewriteEngine On RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.0 RewriteCond %{REMOTE_ADDR} !^x\.x\.x\.x RewriteCond %{QUERY_STRING} script_name [NC] RewriteRule ^(.*)$ index.php [F,L]
...where x.x.x.x is the actual IP address of your server (if you have an SSL certificate, this will be unique to your hosting account - otherwise the IP address might be shared with other hosting accounts on the same server). You can find the IP address of your domain by running a ping test against your domain name (in Windows, go to Start->Run, type in 'cmd' and press enter, then type 'ping www.mydomain.com' and press enter). If you wanted to allow yourself to run the script from a browser (useful for testing), you could add your own IP address as well. Please note, you must add a backslash before each dot of your IP address.