php 如何区分命令行和网络服务器调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/343557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to distinguish command-line and web-server invocation?
提问by Piskvor left the building
Is there a way to distinguish if a script was invoked from the command line or by the web server?
有没有办法区分脚本是从命令行调用还是由 Web 服务器调用?
(See What is the canonical way to determine commandline vs. http execution of a PHP script?for best answerand more detailed discussion - didn't find that one before posting)
(请参阅什么是确定 PHP 脚本的命令行与 http 执行的规范方法?最佳答案和更详细的讨论 - 在发布之前没有找到那个)
I have a (non-production) server with Apache 2.2.10 and PHP 5.2.6. On it, in a web-accessible directory is my PHP script, maintenance_tasks.php. I would like to invoke this script from the command line or through a HTTP request (by opening in a browser). Is there some variable that allows me to reliably determine how script is invoked?
我有一个带有 Apache 2.2.10 和 PHP 5.2.6 的(非生产)服务器。在它的 Web 可访问目录中是我的 PHP 脚本maintenance_tasks.php. 我想从命令行或通过 HTTP 请求(通过在浏览器中打开)调用此脚本。是否有一些变量可以让我可靠地确定脚本的调用方式?
(I already tackled the issues of different views for each type of invocation and HTTP response timeout, just looking for a way of telling the two invocation types apart)
(我已经解决了每种调用和HTTP响应超时的不同视图的问题,只是在寻找一种区分两种调用类型的方法)
I'll be trying different things and add my findings below.
我将尝试不同的事情并在下面添加我的发现。
Duplicate:What is the canonical way to determine commandline vs. http execution of a PHP script?
回答by Shocker
If called from command line, the server variable HTTP_USER_AGENT is not set. I use this constant to define, whether the script is called from command line or not:
如果从命令行调用,则不会设置服务器变量 HTTP_USER_AGENT。我使用这个常量来定义是否从命令行调用脚本:
define("CLI", !isset($_SERVER['HTTP_USER_AGENT']));
define("CLI", !isset($_SERVER['HTTP_USER_AGENT']));
UPDATE:Since this answer is still marked as the 'correct' one, I'd like to revise my statement - relying on the "User-Agent" header can be problematic, since it's a user-defined value.
更新:由于此答案仍被标记为“正确”答案,因此我想修改我的声明 - 依赖“User-Agent”标头可能会出现问题,因为它是用户定义的值。
Please use php_sapi_name() == 'cli'or PHP_SAPI == 'cli', as suggested by Eugene/cam8001 in the comments.
请按照 Eugene/cam8001 在评论中的建议使用php_sapi_name() == 'cli'或PHP_SAPI == 'cli'。
Thanks for pointing this out!
感谢您指出了这一点!
回答by Piskvor left the building
I've compared the $_SERVERsuperglobal in both invocations. It seems that $_SERVER['argc'](i.e. number of arguments passed to the script) is only set when running from shell/command line:
我$_SERVER在两次调用中都比较了superglobal。似乎$_SERVER['argc'](即传递给脚本的参数数量)仅在从 shell/命令行运行时设置:
<?php
if (isset($_SERVER['argc'])) {
define('CLI', true);
} else {
define('CLI', false);
}
That seems to work both on Linux and Windows hosts. (First I thought about checking for some of the environment variables, but those are different for every operating system. Also, all the $_SERVER['HTTP_*']headers are missing in the CLI version, but I'm not sure if that's reliable enough.)
这似乎适用于 Linux 和 Windows 主机。(首先,我考虑检查一些环境变量,但每个操作系统的环境变量都不同。此外,$_SERVER['HTTP_*']CLI 版本中缺少所有标头,但我不确定这是否足够可靠。)

