In most cases you can distinguish if a PHP script was invoked from the command line (or Cron) or by the web server, using php_sapi_name().
php_sapi_name() will return cli on CLI (or Cron) invocation and some other values on invocation by Web Server (e.g. apache2handler for Apache).
Example
A simple example:
<?php
if(php_sapi_name() == 'cli') {
// your code here
} else {
// your code here
}
?>
References
From PHP manual
- php_sapi_name() (PHP 4 >= 4.0.1, PHP 5)
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.