How to check with PHP if the script is being run from the console or browser request?
问题 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? 回答1: Use php_sapi_name() 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