【发布时间】:2011-09-01 20:56:07
【问题描述】:
可能重复:
How to differentiate between http and cli requests?
Which method to detect run mode of php script is more reliable?
确定 php 是从 shell(cron 或其他方式)运行还是在 apache 下运行的最佳方法是什么?
【问题讨论】:
可能重复:
How to differentiate between http and cli requests?
Which method to detect run mode of php script is more reliable?
确定 php 是从 shell(cron 或其他方式)运行还是在 apache 下运行的最佳方法是什么?
【问题讨论】:
【讨论】:
echo php_sapi_name()==="apache2handler"; //running under apache?
【讨论】:
如果您需要知道所使用的确切 SAPI,请参阅 marcelog 的回答。
如果您只需要知道是否有可用的 HTTP 环境,您可以简单地测试$_SERVER['HTTP_HOST']:
$is_web = isset($_SERVER['HTTP_HOST']);
【讨论】: