开发者

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

开发者 https://www.devze.com 2023-03-14 19:01 出处:网络
I now to ways to detect weather php script runs in cli or web server mode: if (defined(\'STDIN\')) or: if (isset($argc))

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开发者_Go百科 is more ?


Neither. Check the value returned from php_sapi_name().


$_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消