0
votes

I have a cron job that runs several times a day at full hours (:00). How can I only allow the PHP scripts to run during this time? I don't want someone else to be able to run my script. Here's what I thought of:

if (date('i', time()) > 2 || date('i', time()) < 58) {
  die;
}

Are there better, more secure ways?

2
"Someone else"? Like whom? If your script is meant only to be run by cron then it shouldn't be accessible from the web (as noted by @jeroen below), which eliminates anyone who doesn't have direct access to your server. That just leaves people who do have direct access to your server, who you should know and trust (or they shouldn't have direct access to your server). - Jordan Running
If someone else can run the script, they can read the script. That means they can copy it, remove your time restrictions, and run it anyways. - Marc B

2 Answers

4
votes

If you place your php script outside of your web directory, only you / cron will be able to run it and nobody else.

There are different ways to run a php script from cron, like for example adding something like this as the first line of your php script:

#!/usr/local/bin/php      /* depends on your server and configuration */
0
votes

The cron jobs are usually run as the root user. You could make your script executable and readable by root only:

sudo chown root script_for_root_only.php
sudo chmod 744 script_for_root_only.php

You can also alter the command in the crontab to run the script as a special user if you do not want to use root..

I think we can assume that you do not have the script in a web accessible location. If you did then move it.