I tried things like $_ENV['CLIENTNAME'] == 'Console' but that seems to work on only certain OS's (worked in windows, not linux).
I tried !empty($_ENV['SHELL']) but that doesn't work always either...
Is there a way to check this that will work in all OS's/environments?
$argc coounts the amount of arguments passed to the command line. Simply using php page.php, $argc will return 1
Calling page.php with a browser, $argc will return NULL
Use
php_sapi_name()
For example:
You can also use the constant
PHP_SAPI
Check the HTTP_USER_AGENT , it should exist in http request
I know this is an old question, but for the record, I see HTTP requests coming in without a User-Agent header and PHP does not automatically define HTTP_USER_AGENT in this case.
Check on http://php.net/manual/en/features.commandline.php#105568 "PHP_SAPI" Constant
One solution is to check whether STDIN is defined: