Protect a cron job from outside access

I recently set up a PHP file that generates an XML-formatted sitemap for a dynamic site and set up a cron job to update the sitemap from time-to-time. I didn’t see any reason to allow outside access to executing the script, so I looked into using .htaccess to restrict access to the script. It turns out that it is pretty easy:

 Order deny,allow
 Allow from 127.0.0.1
 Allow from additional_ip
 Deny from all

You can also add in additional IP addresses if you still want to be able to hit the script from your office or home machine. Just put more IP addresses on their own lines. It is that easy. If you wanted to protect only certain files, you could wrap your rules in something like:

<Files index.html> or use wildcards like: <Files *.php> to block all access to PHP scripts.

Leave a comment

Your email address will not be published. Required fields are marked *