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');
}
In PHP, how to detect the execution is from CLI mode or through browser ?
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.
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!
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()
.
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)));
}
How to determine if a PHP CLI script's output is being piped to another command?
You can use posix_isatty
assuming you have the POSIX extension (you probably do):
posix_isatty(STDOUT)
If it's true, then you are not outputting to a pipe. isatty
is a common method for doing this in C
programs and others.
Note that this doesn't check that it's output to a pipe specifically, just whether it's output to an interactive terminal (a pipe is not one).
PHP wait for input from command line
The php man page for the cli has a comment here detailing a solution, (copied here for anyone else looking)
<?php
echo "Are you sure you want to do this? Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
fclose($handle);
echo "\n";
echo "Thank you, continuing...\n";
?>
Related Topics
Convert Latin1 Characters on a Utf8 Table into Utf8
How to Find the First and Last Date in a Month Using PHP
PHP Fatal Error: Call to Undefined Function Json_Decode()
Using Default Arguments in a Function
Passing an Array as Arguments, Not an Array, in PHP
Checking to See If One Array'S Elements Are in Another Array in PHP
Rebase Array Keys After Unsetting Elements
How to Locate the PHP.Ini File (Xampp)
Create Ini File, Write Values in PHP
How to Access PHP With the Command Line on Windows
Using PHP'S Swiftmailer With Gmail
How to Make a PHP Form That Submits to Self
Can File Uploads Time Out in PHP