0

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

thelolcat
  • 10,995
  • 21
  • 60
  • 102

3 Answers3

1

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)));
}
Community
  • 1
  • 1
iodragon
  • 334
  • 1
  • 4
1

Try this. Whether you have a difference:

if (defined('PHP_SAPI')) {
  echo PHP_SAPI;
}
AterCattus
  • 41
  • 7
0

This is working for me on this box: Debian 7, Apache 2.2, PHP 5.4.4-14+deb7u7 (cgi-fcgi) & PHP 5.4.4-14+deb7u7 (cli)

function is_cli() 
{
    return (
        !array_key_exists('SERVER_SOFTWARE', $_SERVER)
        && (php_sapi_name() === 'cli' || !array_key_exists('REMOTE_ADDR', $_SERVER))
    );
}
Volodymyr Chumak
  • 756
  • 6
  • 12