PHP-如何最好地确定当前调用是来自CLI还是Web服务器? [英] PHP - how to best determine if the current invocation is from CLI or web server?

查看:74
本文介绍了PHP-如何最好地确定当前调用是来自CLI还是Web服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定当前的PHP调用是从命令行(CLI)还是从Web服务器(在我的情况下,是使用mod_php的Apache).

I need to determine whether the current invocation of PHP is from the command line (CLI) or from the web server (in my case, Apache with mod_php).

有推荐的方法吗?

推荐答案

php_sapi_name是您要使用的函数,因为它返回接口类型的小写字符串.此外,还有PHP常量PHP_SAPI.

php_sapi_name is the function you will want to use as it returns a lowercase string of the interface type. In addition, there is the PHP constant PHP_SAPI.

文档可在此处找到: http://php.net/php_sapi_name

例如,要确定是否正在从CLI运行PHP,可以使用以下功能:

For example, to determine if PHP is being run from the CLI, you could use this function:

function isCommandLineInterface()
{
    return (php_sapi_name() === 'cli');
}

这篇关于PHP-如何最好地确定当前调用是来自CLI还是Web服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆