0

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.

1 Answer 1

1

The constant only exist during the lifespan of the script. A Web instance and console instance will not cross paths, as they're running in different processes. It's perfectly save to do what your doing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.