gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:43:17 25 4
gpt4 key购买 nike

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

最佳答案

使用 php_sapi_name()功能。

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

以下是文档中的一些相关说明:

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 >= 4.2.0 中,还有一个预定义常量 PHP_SAPI,其值与 php_sapi_name() 相同。

关于php - 确定 PHP 脚本的命令行与 http 执行的规范方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/173851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com