Which method to detect run mode of php script is more reliable?

雨燕双飞 提交于 2019-12-31 04:27:08

问题


I now to ways to detect weather php script runs in cli or web server mode:

if (defined('STDIN'))

or:

if (isset($argc))

Do they equally reliable or one of them is more ?


回答1:


Neither. Check the value returned from php_sapi_name().




回答2:


$_SERVER['REQUEST_METHOD'] won't be set, due to the lack of a HTTP request. I think defined( 'STDIN' ) or isset( $argc ) are reliable too, though. If it was up to me, I'd probably go with the defined( 'STDIN' ), as I can imagine someone accidentally setting a value to $argc.

The php_sapi_name function above seems to be another (the most reliable?) way of determining this, although I do think it'd be a good idea to read all the gotcha's: mind that different servers will give different answers.



来源:https://stackoverflow.com/questions/6436413/which-method-to-detect-run-mode-of-php-script-is-more-reliable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!