Skip to content

Instantly share code, notes, and snippets.

@mathewjosephh
Created February 21, 2017 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathewjosephh/6d12057ff4830aa25de6cb77805cfe45 to your computer and use it in GitHub Desktop.
Save mathewjosephh/6d12057ff4830aa25de6cb77805cfe45 to your computer and use it in GitHub Desktop.
How to allow a script to be run from a Cron Job but not from a Browser
Method 1:
---------
If you are executing PHP directly from the Cron Job e.g. php /path/to/your_script.php then add the following line at the top of your PHP script:
if (php_sapi_name() !='cli') exit;
Method 2:
---------
if the Cron Job uses wget, curl or lynx to run your script via its URL, then insert this code at the top of your PHP script (change the User Agent string to one known only by you) :
if ($_SERVER['HTTP_USER_AGENT'] != 'yourSecretAgent') exit;
You will also have to set the User Agent in the Cron Job; as in this wget example:
wget -O - --user-agent=“yourSecretAgent” http://example.com/your_script.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment