How to detect if PHP was launched from CLI (i.e. command line, console)
Often we write some console applications that are run by cron.
In these cases we have to check if the script is executed from the command line or it's a normal HTTP request. If it's a command line call then it doesn't make sense to ask for user and password and redirect to a login screen and other validation checks.
Here is how to check if the current php script was started from the command line.
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cli') {
echo "You are using CLI PHP (command line, console)\n";
} else {
echo "You are using something else\n";
}
?>
Related:
http://us.php.net/manual/en/function.php-sapi-name.php

0 comments:
Post a Comment