I need to determine whether the PHP file is being loaded via cron or command line within the code. How can I do this?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You can check the
PHP_SAPI
constant to check if the CLI interpreter is being used:$is_cli= PHP_SAPI == 'cli';
The most reliable and exhaustive way to check where your script is run known to me is
php_sapi_name()
Neither this nor any of the other listed methods listed here, however, will give you a distinction between "normal" CLI mode, and a cron call. gahooa's command line argument idea is probably the best and most reliable solution.
This is one simple way. Certain elements of the
$_SERVER
array are only set if called from HTTP. Thus you can:Others include:
$_SERVER['HTTP_HOST']
If you have control over the cron or command, have you considered passing a command-line argument, and reading it with
$_SERVER['argv'][0]
?In the script: