php_sapi_name()的替代方案对Google App Engine有用吗?

How can I determine whether a script is running from CLI or CGI (inc. Google App Engine), without using php_sapi_name()?

EDIT: Without relying on undefined behaviour.

Google App Engine says:

 php_sapi_name() has been disabled for security reasons

and though I can re-enable it in php.ini, I would prefer not, esp. since I can't find the security reasons stated.

EDIT: if(isset($argv[1])) appears to work, but relies on undefined behaviour of $argv in CGI. http://php.net/manual/en/reserved.variables.argv.php .

Check the $_SERVER superglobal Variable for one of the following values:

  • REQUEST_METHOD
  • SERVER_ADDR
  • SERVER_SOFTWARE
  • SERVER_NAME

like this:

if (!isset($_SERVER['SERVER_ADDR'])) {
     echo 'this is run on the cli';
}

If you want to check for the google-app engine try $_SERVER['APPLICATION_ID']; according to their docs: https://cloud.google.com/appengine/docs/php/#PHP_Special_server_keys