Command Line Alternative

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.

[Note]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.

Securing the script

We earlier mentioned the use of a secret key to be provided when the front end is accessed. We have not implemented such a mechanism for the CLI script. Instead we make use of the htaccess file. The PHP script has to be called using HTTP, and needs to be able to load the web environment, so it cannot be hidden away in a private part of your server's directory structure, and has to be called through Joomla. This opens up the risk that anyone could come along and type the URL into their browser - thus causing the script to be run independently of CRON. The easiest way to protect against this (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]Note

For the following rules to work, you must have an Apache web server with mod_rewrite enabled.

[Note]Note

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)..Please note, you must add a backslash before each dot of your IP address.

Go To Top

Joomla! Debug Console

Session

Profile Information

Memory Usage

Database Queries