Page 1 of 1

Detect php cli execution

Posted: Thu Mar 22, 2007 6:41 pm
by WaldoMonster
I have a web-based script than I can manually update.
For the web interface you must login.
But I want the same script run as a scheduled cli script.
To defeat the login procedure it must reliable detect that the script is executed from the cli.
Is there a reliable way to detect if the script is executed from the cli?

Posted: Thu Mar 22, 2007 6:46 pm
by feyd

Posted: Thu Mar 22, 2007 7:27 pm
by WaldoMonster
Didn't you mean php_sapi_name() instead?
But this returned "cgi-fcgi" from the cli.
It could also be from a web server.

Detecting if one of the following values are set:

Code: Select all

$_SERVER['REQUEST_METHOD'];
$_SERVER['SERVER_PROTOCOL'];
$_SERVER['SERVER_PORT'];
Or is this not reliable?

Posted: Thu Mar 22, 2007 7:30 pm
by feyd
Err, yeah.. php_sapi_name(). If it says it's CGI then it's not the CLI version.

Often, the existence of $argv ($_SERVER['argv']) and $argc ($_SERVER['argc']) are decent detectors. Especially if you pass some data to the script when running it from the CLI.

Posted: Thu Mar 22, 2007 7:40 pm
by WaldoMonster
I found the right php.exe, in the cli map.
Now php_sapi_name() gives the expected result.
Thanks for the help.