dongpingwu8378 2013-08-09 13:49
浏览 61
已采纳

检查接口类型是否为PHP中的CLI

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?

  • 写回答

1条回答 默认 最新

  • dqjmq28248 2013-08-09 14:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存