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
Is There a PHP Library For Email Address Validation
PHP: Merge 2 Multidimensional Arrays
Causes of MySQL Error 2014 Cannot Execute Queries While Other Unbuffered Queries Are Active
Save Image from Url With Curl PHP
How to Install MySQLi on Macos
How to Resolve Curl Error (7): Couldn't Connect to Host
Send Value of Submit Button When Form Gets Posted
PHP Json_Decode() Returns Null With Valid Json
MySQLi_Query() Expects Parameter 1 to Be MySQLi, Object Given
Fastest Way to Serve a File Using PHP
Is Possible to Keep Session Even After the Browser Is Closed
How to Show Multiple Recaptchas on a Single Page
PHP Foreach Pass by Reference: Last Element Duplicating (Bug)
How to List All Months Between Two Dates
Pass a Variable to a PHP Script Running from the Command Line