• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

PHP Include Question

I require a config.php file at the top of every page on my site.
I am using this method:
<?php require $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';?>

Though, sometimes there is a subdomain (which the document root would be different), and instead of hard coding a path in, I would rather use one of the server variables that is good site wide.

Do you have a different suggestion for one that I can use? I looked into PHPRC, but to me that seems where the php.ini file location is, right?

What do you recommend?


I also want to do this so I can have the same code on every page whether I'm live or local, is that doable or do you have a different suffestion?

require $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';
else if $_SERVER['SERVER_ADDR'] = ""
require '/includes/config.php';
else echo "You have problems!"

Open in new window

Computer Guy
Computer Guy
1 Solution
Julian HansenCommented:
Would you want the same config file for the sub-domain as for the main site?

If so then you are going to have to code your sub-domain page to pickup the config file from a location relative to the parent.

One strategy is to define a BASEPATH variable in the landing script and then prepend this to your path i.e.

define('BASEPATH', dirname(__FILE__) );

Open in new window


Open in new window

And then
require_once(BASEPATH . 'includes/config.php')

Open in new window

Not sure why using the DOCUMENT_ROOT var is necessary as you could get to the file using relative paths anyway.

In terms of part 2 personally I prefer to keep the config in the same place and then just have a different config for different environments.

In other words


Open in new window

Two strategies here

1. Keep the config.php in the root folder

2. Keep the config.php outside of the root as a security measure i.e


Open in new window

The above means your config settings cannot be accessed through the HTTP connection directly.
Ray PaseurCommented:
Avoid DOCUMENT_ROOT.  It's quite variable from one environment to the next.  If you have CRON or email PIPE scripts, you will find that the base path for those scripts is outside the WWW root, so your includes and such will need to change for those, too.

If you keep the config file in the WWW root, make sure it cannot emit any browser output.

You can get a little more portability if you use DIRECTORY_SEPARATOR instead of hard-coding the slash.  And you can save some typing if you define('DS', DIRECTORY_SEPARATOR);

See also this link.  A response there would have been helpful, and it would still be helpful here, too!
Computer GuyAuthor Commented:

Will this "define('BASEPATH', dirname(__FILE__) );" mess with the HTML BASEPATH that I will have in each page?

Also, to do the base path with say live website and local site, what server variable can I use that would work with minimal future problems?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Computer GuyAuthor Commented:
Sure, that's great, but what variable would not change much? an IP?
Ray PaseurCommented:
It's going to be installation-dependent.   PHP has about a million variables and initialization settings, and you can't control for all of them.  You have a laudable goal, and it is a research project to get to the answer.  I can show you the roadmap, but I can't make the journey for you because I don't have access to your PHP installations.

From the other similar question:

What you're doing is a good idea, but achieving it may be a little more involved than you know right now.  I can't answer this for you but I can show you how to find the answer.
First, please be careful to get the terminology right. This link shows the words and their definitions.  Subdirectories are often called "paths" in the PHP online docs.

Next run the following script (shown here in its entirety) on each of your installations in each of the subdomains and subdirectories, as well as the web root directory (note the terminology carefully).  Print the outputs so you can sit down in a chair and look at all of them at the same time.  Look for consistencies and differences in the SERVER vars.

<?php echo '<pre>'; var_dump($_SERVER);

Open in new window

Then consult with your web host about the organization of the request and any URL rewriting that may be in place.  This matters because some hosting companies will be "helpful" to you by rewriting all domain.com requests to www.domain.com or vice versa.  Consider what will happen if a client comes to your site with FFF.domain.com -- will your .htaccess rewrite that request?

Be aware that HTTP cookies, including the session cookie, are by default tied to a single subdomain and a single subdirectory.  You can set cookies that work across subdomains and subdirectories if you look at the options in setcookie().

The path to your data base server, even if it's on localhost, may vary from one installation to the next, so check that, too.

Check also for the SERVER vars in any CRON or email PIPE scripts.  It will almost certainly be different from the www root.

If you ever change hosting companies, you should plan on rechecking everything.

And finally, good luck! ~Ray
Computer GuyAuthor Commented:
Say I wanted to use an IP address (which I have control over it as I'm paying for a dedicated IP), would that be a good road to go down?

Or do you have any other suggestions?

array(35) {
  string(5) "Basic"
  string(25) "/home3/username/public_html"
  string(7) "CGI/1.1"
  string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  string(17) "gzip,deflate,sdch"
  string(14) "en-US,en;q=0.8"
  string(9) "max-age=0"
  string(10) "keep-alive"
  string(11) "domain.com"
  string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  string(13) "/bin:/usr/bin"
  string(13) "/home3/username"
  string(0) ""
  string(3) "200"
  string(12) ""
  string(4) "4653"
  string(5) "admin"
  string(3) "GET"
  string(9) "/file.php"
  string(34) "/home3/username/public_html/file.php"
  string(9) "/file.php"
  string(14) ""
  string(21) "webmaster@domain.com"
  string(11) "domain.com"
  string(2) "80"
  string(8) "HTTP/1.1"
  string(56) "
Apache Server at domain.com Port 80

  string(6) "Apache"
  string(24) "Ul-ZysC5BCgAABrBpBwAAAVW"
  string(9) "/file.php"
  array(0) {
Ray PaseurCommented:
This is only one list from $_SERVER.  I think the process would be to compare the $_SERVER lists from different subdomains and subdirectories and look for patterns that would enable you to make a sensible choice.  A common-sense structure of the subdomains (for me, at least) might be like this:

www.domain.com rewrites to domain.com
anything-else-at-all.domain.com rewrites to domain.com

The comparison process would consider these URLs

Computer GuyAuthor Commented:
How about this to make it easier:

If Local host
Computer GuyAuthor Commented:
<?php if ($_SERVER['SERVER_NAME'] == "localhost")
require 'config.php';
require 'home/user/config.php';
Ray PaseurCommented:
Like I said earlier, there are a lot of moving parts to this question!  You would want to set up a test script and run it in all of the environments, and see what changes in the subdomains or paths cause in the script's environment.  All I can do is warn you of known pitfalls and help guide your research; I can't tell you if one approach or another will work perfectly in all of your environments.

Please check these links.
Computer GuyAuthor Commented:
Ok, let me take a step back here. I was giving some information that I always thought was static.

This site will be either local or live on 1 domain.com with multiple subdomains.

The path structure will be:


Which when I look at my phpinfo file, the server_name is either domain.com or subdomain.domain.com

So if I look for local host first and it fails, then it will be hard coded to:
require /home/user/config.php (since this will be accessible to a domain.com or subdomain.domain.com.

1. If (in theory) what I said was accurate, would it work?
2. Is it common coding practice to put the failing part first (if server = localhost), or does it not matter?
Ray PaseurCommented:
1. I think so.  Only you can test the theory, but it looks right enough to me that it would be worth making the test.

2. Does not matter.  But remember that a single equal sign is an assignment operator.  A comparison for equality is a double equal sign.
Dave BaldwinFixer of ProblemsCommented:
The only thing like that that I ever do is to check the IP address to see if it is on my local network.  Other than that, I Always use relative page addresses to make it 'portable' between my servers and the hosting servers.  While there are some things that don't want to be portable, I just avoid them as much as possible.  The more of these variable variable things you put into your code, the harder it is to maintain and modify it.

Also, you can Count on the $_SERVER array info to be different on different servers.  Hosting companies sometimes have peculiar ideas of what info should be in the $_SERVER array and block or add info as they see fit.  While there should be a lot of common info each time, you need to test it before you try to count on it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now