What Is the Canonical Way to Determine Commandline Vs. Http Execution of a PHP Script

What is the canonical way to determine commandline vs. http execution of a PHP script?

Use the php_sapi_name() function.

if (php_sapi_name() == "cli") {
// In cli-mode
} else {
// Not in cli-mode
}

Here are some relevant notes from the docs:

php_sapi_name — Returns the type of interface between web server and PHP

Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, cli-server, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

In PHP >= 4.2.0, there is also a predefined constant, PHP_SAPI, that has the same value as php_sapi_name().

Can I php script know if it's called from command line or web server

From php.net

if (PHP_SAPI === 'cli') 
{
// ...
}

how to determine if php is being run from command line

I would like to know if its possible within the php script to determine if its being run via command line?

Definitely. Check for the output of php_sapi_name( ), it will tell you whether or not you're running in CLI mode.

$cli = php_sapi_name( ) === 'cli';
var_dump( $cli );

PHP - how to best determine if the current invocation is from CLI or web server?

php_sapi_name is the function you will want to use as it returns a lowercase string of the interface type. In addition, there is the PHP constant PHP_SAPI.

Documentation can be found here: http://php.net/php_sapi_name

For example, to determine if PHP is being run from the CLI, you could use this function:

function isCommandLineInterface()
{
return (php_sapi_name() === 'cli');
}

How to distinguish command-line and web-server invocation?

If called from command line, the server variable HTTP_USER_AGENT is not set. I use this constant to define, whether the script is called from command line or not:

define("CLI", !isset($_SERVER['HTTP_USER_AGENT']));

UPDATE: Since this answer is still marked as the 'correct' one, I'd like to revise my statement - relying on the "User-Agent" header can be problematic, since it's a user-defined value.

Please use php_sapi_name() == 'cli' or PHP_SAPI == 'cli', as suggested by Eugene/cam8001 in the comments.

Thanks for pointing this out!

how to make a PHP file run-able only through CLI mode?

See:

  • What is the canonical way to determine commandline vs. http execution of a PHP script?
  • Is there any way to know if a php script is running in cli mode?
  • PHP - what is the best & easy way to determine if the current invocation is from CLI or browser

Short story: php_sapi_name().

How to tell if PHP is running under a browser or was invoked as a standalone script

If I understand your question correctly, it's not about http vs. command line call, but rather browser vs. "non-browser" (e.g. via curl, wget etc) call. There's no way to check this, because wget etc are technically browsers, they just don't happen to have a GUI. You can try checking HTTP_USER_AGENT, but this is totally unreliable, because there's no way to enforce a client to identify itself correctly.

Distinguish from command line and CGI in python

The Perl answer applies to Python as well; check for the GATEWAY_INTERFACE environment variable:

import os
if 'GATEWAY_INTERFACE' in os.environ:
print ('CGI')
else:
print ('Not CGI. CLI?')

How to detect if in CLI mode with cgi-fcgi

Check the $_SERVER and the $_ENV superglobal arrays.

Try $_SERVER['REMOTE_ADDR'] or $_SERVER['argc'].

I found a good answer form https://stackoverflow.com/a/12654906/1197702

function drupal_is_cli() {
return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
}


Related Topics



Leave a reply



Submit