Check if php is running from command line interface (cli)
Inside a script it might be necessary to test if it is being run from command line or not. For example when a script is being called from cron it might produce a different kind of output than when it is run from a browser url. In such cases the script needs to identify its running mode.
Detects whether the current script is running in a command-line environment.
example 1.
function is_cli()
{
if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)
{
return true;
}
return false;
}
Add an STDIN check.
function is_cli()
{
if( defined("STDI"') )
{
return true;
}
if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)
{
return true;
}
return false;
}
example 2.
function is_cli() {
return (!isset($_SERVER["SERVER_SOFTWARE"]) && (php_sapi_name() == "cli" || (is_numeric($_SERVER["argc"]) && $_SERVER["argc"] > 0)));
}