0

I've have a cron file, monthly.php and I want to prevent direct access using web browser. It should be accessible only through CPanel cron.

Thanks.

Ajay
  • 715
  • 5
  • 12
  • 23
  • possible duplicate of [Can PHP detect if its run from a cron job or from the command line?](http://stackoverflow.com/questions/190759/can-php-detect-if-its-run-from-a-cron-job-or-from-the-command-line) – Your Common Sense Oct 03 '11 at 13:50

4 Answers4

5

Don't put it under the webroot. Just execute it using the command line php program.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

You can use a .htaccess to deny access to it. Or you can just move it out of the htdocs or public_html directory.

<Files "cron.php">
  Order deny,allow
  Allow from name.of.this.machine
  Allow from another.authorized.name.net
  Allow from 127.0.0.1
  Deny from all
</Files>

So it can only be requested from the server.

Mob
  • 10,958
  • 6
  • 41
  • 58
1

If you, for some reason, need to put it in a webroot, try the following: Can PHP detect if its run from a cron job or from the command line?

Community
  • 1
  • 1
ChrisH
  • 1,283
  • 1
  • 9
  • 22
0

Just pass in a key to it to protect it. And don't report "Key parameter is missing" to the browser, just die() if the key is not there. And please, dont use the parameter "key", use something of your own like:

http://myscript.com/monthly.php?mycomplexkeyname=ksldhfguorihgiauzsiludrfthgo45j1234134
Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71