确定 PHP 脚本的命令行与 http 执行的规范方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/173851/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:47:05  来源:igfitidea点击:

What is the canonical way to determine commandline vs. http execution of a PHP script?

phphttpcommand-linecommand-line-interface

提问by Bobby Hyman

I have a PHP script that needs to determine if it's been executed via the command-line or via HTTP, primarily for output-formatting purposes. What's the canonical way of doing this? I had thought it was to inspect SERVER['argc'], but it turns out this is populated, even when using the 'Apache 2.0 Handler' server API.

我有一个 PHP 脚本,需要确定它是通过命令行还是通过 HTTP 执行的,主要用于输出格式。这样做的规范方法是什么?我原以为是检查SERVER['argc'],但事实证明这是填充的,即使在使用“Apache 2.0 处理程序”服务器 API 时也是如此。

回答by

Use the php_sapi_name()function.

使用该php_sapi_name()功能。

if (php_sapi_name() == "cli") {
    // In cli-mode
} else {
    // Not in cli-mode
}

Here are some relevant notes from the docs:

以下是文档中的一些相关注释:

php_sapi_name— Returns the type of interface between web server and PHP

Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, cli-server, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

php_sapi_name— 返回 Web 服务器和 PHP 之间的接口类型

尽管并非详尽无遗,但可能的返回值包括 aolserver、apache、apache2filter、apache2handler、caudium、cgi(直到 PHP 5.3)、cgi-fcgi、cli、cli-server、continuous、embed、isapi、litespeed、milter、nsapi、phttpd、 pi3web、roxen、thttpd、tux 和 webjames。

In PHP >= 4.2.0, there is also a predefined constant, PHP_SAPI, that has the same value as php_sapi_name().

在 PHP >= 4.2.0 中,还有一个预定义的常量PHP_SAPI,它与 具有相同的值php_sapi_name()

回答by Xeoncross

This will always work. (If the PHP version is 4.2.0 or higher)

这将始终有效。(如果PHP版本为4.2.0或更高)

define('CLI', PHP_SAPI === 'cli');

Which makes it easy to use at the top of your scripts:

这使得在脚本顶部使用起来很容易:

<?php PHP_SAPI === 'cli' or die('not allowed');

回答by ya.teck

Here is Drupal 7 implementation: drupal_is_cli():

这是 Drupal 7 实现:drupal_is_cli()

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

However Drupal 8 recommendsusing PHP_SAPI === 'cli'

但是 Drupal 8建议使用PHP_SAPI === 'cli'

回答by Vinko Vrsalovic

I think

我认为

$_SERVER['REMOTE_ADDR']

will not be populated from the CLI.

不会从 CLI 填充。

Also, all the HTTP_* keys in the $_SERVER superglobal won't be populated from the CLI, or do it the right way hop just mentioned :-)

此外,$_SERVER 超全局变量中的所有 HTTP_* 键都不会从 CLI 中填充,或者按照刚才提到的正确方式进行 :-)

回答by Steve

The documentation page for php_sapi_name clearly states how it works:

php_sapi_name的文档页面清楚地说明了它是如何工作的:

Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using....

Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

返回描述 PHP 使用的接口类型(服务器 API,SAPI)的小写字符串。

尽管并非详尽无遗,但可能的返回值包括 aolserver、apache、apache2filter、apache2handler、caudium、cgi(直到 PHP 5.3)、cgi-fcgi、cli、continuous、embed、isapi、litespeed、milter、nsapi、phttpd、pi3web、roxen、 thttpd、tux 和 webjames。

I'm not sure why hop doesn't think that PHP is for serious programmers (I'm a serious programmer, and I use PHP daily), but if he wants to help clarify the documentation then perhaps he can audit all possible web servers that PHP can run on and determine the names of all possible interface types for each server. Just make sure to keep that list updated as new web servers and interfaces are added.

我不确定为什么 hop 不认为 PHP 适合认真的程序员(我是认真的程序员,我每天都使用 PHP),但是如果他想帮助澄清文档,那么也许他可以审核所有可能的 Web 服务器PHP 可以运行并确定每个服务器的所有可能的接口类型的名称。只要确保在添加新的 Web 服务器和接口时更新该列表。

Also, Bobby said:

另外,鲍比说:

I'm intrigued as to why the doc. example inspects the first 3 characters, whilst the description states the string should be exactly "CGI"

我很好奇为什么医生。示例检查前 3 个字符,而描述说明字符串应该完全是“CGI”

The description for the example states:

该示例的描述指出:

This example checks for the substring cgi because it may also be cgi-fcgi.

此示例检查子字符串 cgi,因为它也可能是 cgi-fcgi。