I now to ways to detect weather php script runs in cli or web server mode:
if (defined(\'STDIN\'))
or:
if (isset($argc))
Neither. Check the value returned from php_sapi_name().
php_sapi_name()