How to check if PHP script is running by shell
php
Solution
Use the `php_sapi_name` function:
if(php_sapi_name() == 'cli')
{
// running from CLI
}
From the Manual:
Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using. For example, in CLI PHP this string will be "cli" whereas with Apache it may have several different values depending on the exact SAPI used. Possible values are listed below.
Problem
Possible Duplicate: In php, how to detect the execution is from CLI mode or through browser? How can I check if a PHP script is running by Shell (command line) or by a server?