Can I change the value of $ _SERVER ['DOCUMENT_ROOT']?

I am setting up a test environment for a client project for an application that has already been programmed by someone else. I created a subdirectory called iftc in the hosting account, which we usually use for such purposes.

Now all included files are not found, because they are referenced via

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

And so on.

With the exception of setting up the entire new hosting account for testing purposes only for this particular client, can I change the value of $_SERVER['DOCUMENT_ROOT'] some way to include the iftc subfolder where the files are located?

+4
source share
3 answers

My preferred solution

There are several ways to do this, but it’s best to just find and replace all uses of $_SERVER['DOCUMENT_ROOT'] simple function call.

Thus, your example will be as follows:

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

Determine the current mode of operation:

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

Now to define the function:

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

Overriding the actual values ​​in $_SERVER is a bad idea. If someone else starts working on the project later, it will not be clear what is happening.

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

Where you can't do it

Another way to do it

When I set up my massive virtual development environment, I ran into this problem. See http://blog.simonholywell.com/post/1516566788/team-development-server#virtual_document_root

Since I could not override $_SERVER['DOCUMENT_ROOT'] using any of the above methods, I had to do this in auto_prepend_file .

I would not recommend using this method to solve this particular problem, but in this case it is better solved at the application level.

+8
source

You cannot change the DOCUMENT_ROOT environment variable to PHP. (If you are not playing with a wrapper CGI script).
Apache has the SetEnv directive, but this will not work for DOCUMENT_ROOT (CGI env special requirement). Could give it an alternate name, but DOC_ROOT2 , etc.

But you can globally override this variable in PHP using the php.ini parameter auto_prepend_file , which can also be set using .htaccess:

 php_value auto_prepend_file ./override_docroot.php 

And that the script will then "globally" adapt your environment:

 <?php $_SERVER["DOCUMENT_ROOT"] = "..."; 
+1
source

This is configured through the web server, not through PHP. For example, in Apache, this is the DocumentRoot directive.

Why don't you use relative paths? You can be completely independent of where the application is located if you use paths like ../Includes/Connect.php .

0
source

Source: https://habr.com/ru/post/1384424/


All Articles