且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PHP中,如何检测是从CLI模式还是通过浏览器执行?

更新时间:2021-09-23 16:54:04

使用 php_sapi_name() 函数. /p>

Use the php_sapi_name() function.

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

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

Here are some relevant notes from the docs:

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

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

尽管不详尽,但可能的返回值包括aolserver,apache,apache2filter,apache2handler,caudium,cgi(直到PHP 5.3),cgi-fcgi,cli,cli-server,连续性,嵌入,isapi,litespeed,milter,nsapi ,phttpd,pi3web,roxen,thttpd,tux和webjames.

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.