How to check with PHP if the script is being run from the console or browser request?

后端 未结 6 1537
孤街浪徒
孤街浪徒 2021-02-02 05:58

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\']) b

6条回答
  •  难免孤独
    2021-02-02 06:41

    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 different values depending on the exact SAPI used.

    For example:

    $isCLI = ( php_sapi_name() == 'cli' );
    

    You can also use the constant PHP_SAPI

提交回复
热议问题