In my web-application, I have the following constant:
//set global path if not yet set
if(!defined('FILE_ROOT_PATH')){
define('FILE_ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
}
so the code is portable and I can include/require with absolute paths.
Now there are some PHP-files which have to be executable from CLI and CGI, and of course $_SERVER is not available from CLI.
So what I did is change it to this code in those special files that are executed with CLI and CGI:
//set global path if not yet set and set it via dirname for CLI
if(!defined('FILE_ROOT_PATH') && strlen($_SERVER['DOCUMENT_ROOT'])){
define('FILE_ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
}else{
define('FILE_ROOT_PATH', dirname(dirname(__FILE__)));
}
so CGI will execute the first "define" and CLI the 2nd.
Is that safe to do so? When executed on CLI, the constant won't be "overwritten" for CGI, right? Thanks for your thoughts.