When placing the configuration file outside the public root. as I call it on every page without defining a path

this is how my file structure is. [..] are folders

[public] [libs] [crontab] [logs] [sessions] [tmp] config.php 

the domain is installed in the shared folder as its root. everything else, including the config.php file, is outside the root. config contains db connection information and other necessary settings.

I have to put every page in the shared folder all the way, so include the configuration file. the problem is when I move everything from the local machine to a public server, I have to go through each page and change the path included for the configuration file. Is there a better way? perhaps setting the path in php.ini or something like that?

thanks

+4
source share
4 answers

In this particular case, you can use:

 include("$_SERVER[DOCUMENT_ROOT]/../config.php"); # Note: special exception array syntax within double quotes. 

First, it resolves before the public directory, but moves one level back ../ to get to the configuration file outside of docroot.


But you can also use SetEnv in your .htaccess to define another environment variable $_SERVER for the case.

+3
source

The method that I often use to include files with respect to a specific location in your website structures is to use include / require as follows:

require_once(dirname(__FILE__) . '/path/to/include.php');

Thus, to transfer code bases, it is not necessary to change any hard links or constants if the files remain relative to each other in the same way.

+1
source

You can simply use a constant for your path and change it once and define this constant for each file with the path included

You can also see the inclusion path http://php.net/manual/en/function.set-include-path.php

0
source

See set_include_path() and the docs for include_path ini.

0
source

All Articles