可能重複:
How to differentiate between http and cli requests?
Which method to detect run mode of php script is more reliable?確定PHP是從shell還是在Apache中運行的方法?
什麼,以確定是否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還是在Apache中運行的方法?
什麼,以確定是否PHP是從shell(cron或其他方式)或運行在Apache運行的最佳方法是什麼?
echo php_sapi_name()==="apache2handler"; //running under apache?
見marcelog的答案,如果你需要知道確切的SAPI被使用。
如果你需要知道的是你是否有可用的HTTP環境下,你可以簡單地測試$_SERVER['HTTP_HOST']
:
$is_web = isset($_SERVER['HTTP_HOST']);
http://stackoverflow.com/questions/2172970/how-to-differentiate-間的HTTP和CLI-請求 – ajreal