Search code examples
phpapacheexeccommand-line-interfacefastcgi

How to detect if in CLI mode with cgi-fcgi


php_sapi_name() returns cgi-fcgi in both CLI and web :(

Is there any reliable way to detect if the script is current running in command line mode when PHP is running with fcgi?

edit: nvm. it turns out I had to run the php-cli executable lol


Solution

  • Check the $_SERVER and the $_ENV superglobal arrays.

    Try $_SERVER['REMOTE_ADDR'] or $_SERVER['argc'].

    I found a good answer form https://stackoverflow.com/a/12654906/1197702

    function drupal_is_cli() {
      return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
    }