Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to know if a php script is running in cli mode?

... or the other way around, is there any way to know if a php script is running inside a web server?

like image 797
Andrei Savu Avatar asked Mar 03 '09 17:03

Andrei Savu


2 Answers

http://www.php.net/manual/en/function.php-sapi-name.php

function is_cli()
{
    return php_sapi_name() === 'cli';
}
like image 58
Ionuț G. Stan Avatar answered Oct 08 '22 05:10

Ionuț G. Stan


Typically, when running in CLI mode, the superglobals $argv and $argc will be set, and many of the typical contents of $_SERVER (e.g. request method) won't be available. In addition, pre-defined console streams such as STDIN, STDOUT and STDERR will be set up.

like image 30
Rob Avatar answered Oct 08 '22 06:10

Rob