Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

php - can I change value of $_SERVER['DOCUMENT_ROOT']?

I am setting up a testing environment for a client project for an application that was already programmed by someone else. I have created a subdirectory called iftc in the hosting account that we normally use for such purposes.

Now, all include files are not being found as they are being referenced through

include($_SERVER['DOCUMENT_ROOT'].'/Includes/Connect.php'); 

And so on.

Short of setting up a whole new hosting account just for testing purposes for this particular client, can I change the value of $_SERVER['DOCUMENT_ROOT'] somehow to include a subfolder iftc that the files are in?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My preferred solution

There are a couple of ways to do it but the best is to simply find and replace all uses of $_SERVER['DOCUMENT_ROOT'] with a simple function call.

So your example would become:

include(get_my_path() . '/Includes/Connect.php');

Define your current run mode:

define('RUN_MODE_PRODUCTION', true); // in live mode
define('RUN_MODE_PRODUCTION', false); // debug mode

Now for the function definition:

function get_my_path() {
    if(RUN_MODE_PRODUCTION === true) {
        return '/my/path/';
    }
    return '/my/other/path';
}

Overriding the actual values in $_SERVER is bad idea. Should some one else later come to work on the project it will not be clear what is happening.

This is a very simplified version of the bootstrapping of environments that I use in production every day.

Where you can't do it

Another way you can do it

When I setup my mass virtual environment for developing I encountered this issue. See http://blog.simonholywell.com/post/1516566788/team-development-server#virtual_document_root

Because I could not override $_SERVER['DOCUMENT_ROOT'] using either of the above methods I had to do it in an auto_prepend_file.

I would not recommend that you use this technique to solve this particular issue however as it is better solved at the application level in this case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...