Chords
Chords

Reputation: 6850

Checking if Interface Type is CLI in PHP

It looks like there are two typical ways to determine if a script is running via command line.

  1. if ( defined('STDIN') ) # Running via CLI
  2. if (PHP_SAPI === 'cli') # Running via CLI

Since they're both used, and recommended, I assume they both work well. My question is: are there any fringe cases where one would fail? Which is preferred, and why?

Upvotes: 1

Views: 532

Answers (1)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

I wouldn't rely on if (defined('STDIN')) ever. Simply because, if the STDIN constant isn't defined, I can define it myself:

defined('STDIN') || define('STDIN', 'random value');

that would have any of the following code, using if (defined('STDIN')) believing it's running on the command line, whereas PHP_SAPI will always be defined.
Another thing you have to take into account is namespaces. Sure, this affects both checks, but you could also use the php_sapi_name function to be sure, or simply:

if (\PHP_SAPI === 'cli')

Which, to my eye, is more readable than:

if (defined('\\STDIN'))

So basically, use any of the three options, but be aware that STDIN can be a user-defined constant, too. PHP_SAPI or the return value of php_sapi_name is always going to be astring (AFAIK), and you know what value to expect when running a CLI script: 'cli'. Though, if for some reason you don't trust that, I have seen people do this:

if (stristr(PHP_SAPI, 'cli') && strpos(PHP_SAPI, 'cli') === 0)

But that's just paranoid, IMHO.
Some just check $argv, but that implies the register_argc_argv setting is enabled, which it is by default, but it can be disabled, which you could, in turn, counter with something like:

#!/var/bin/php -n
<?php
    if (!is_array($argv))
    {
        exit();
    }

by using /path/to/php -n, you're not using any ini file, so default settings will be used, and the since the default setting for register_argc_argv is 1, $argv will be available.

Upvotes: 1

Related Questions