Link to home
Start Free TrialLog in
Avatar of Computer Guy
Computer Guy

asked on

PHP Include Question

I require a config.php file at the top of every page on my site.
PART 1:
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?

PART 2:

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?

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

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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.

<?php
define('BASEPATH', dirname(__FILE__) );
?>

Open in new window

OR
<?php
define('BASEPATH','../');
?>

Open in new window


And then
<?php
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

<?php
require_once('config.php');
...
?>

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
/path
      /to
        /config
            config.php
        /document_root

<?php
require_once('../config/config.php');
?>

Open in new window


The above means your config settings cannot be accessed through the HTTP connection directly.
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!
https://www.experts-exchange.com/questions/28261164/PHP-Operator-Help.html?anchorAnswerId=39576454#a39576454
Avatar of Computer Guy
Computer Guy

ASKER

Hi,

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?
Sure, that's great, but what variable would not change much? an IP?
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.
https://www.experts-exchange.com/questions/28261164/PHP-Operator-Help.html?anchorAnswerId=39576454#a39576454

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
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) {
  ["AUTH_TYPE"]=>
  string(5) "Basic"
  ["DOCUMENT_ROOT"]=>
  string(25) "/home3/username/public_html"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["HTTP_ACCEPT"]=>
  string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  ["HTTP_ACCEPT_ENCODING"]=>
  string(17) "gzip,deflate,sdch"
  ["HTTP_ACCEPT_LANGUAGE"]=>
  string(14) "en-US,en;q=0.8"
  ["HTTP_CACHE_CONTROL"]=>
  string(9) "max-age=0"
  ["HTTP_CONNECTION"]=>
  string(10) "keep-alive"
  ["HTTP_HOST"]=>
  string(11) "domain.com"
  ["HTTP_USER_AGENT"]=>
  string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  ["PATH"]=>
  string(13) "/bin:/usr/bin"
  ["PHPRC"]=>
  string(13) "/home3/username"
  ["QUERY_STRING"]=>
  string(0) ""
  ["REDIRECT_STATUS"]=>
  string(3) "200"
  ["REMOTE_ADDR"]=>
  string(12) "79.158.7.123"
  ["REMOTE_PORT"]=>
  string(4) "4653"
  ["REMOTE_USER"]=>
  string(5) "admin"
  ["REQUEST_METHOD"]=>
  string(3) "GET"
  ["REQUEST_URI"]=>
  string(9) "/file.php"
  ["SCRIPT_FILENAME"]=>
  string(34) "/home3/username/public_html/file.php"
  ["SCRIPT_NAME"]=>
  string(9) "/file.php"
  ["SERVER_ADDR"]=>
  string(14) "192.185.32.182"
  ["SERVER_ADMIN"]=>
  string(21) "webmaster@domain.com"
  ["SERVER_NAME"]=>
  string(11) "domain.com"
  ["SERVER_PORT"]=>
  string(2) "80"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["SERVER_SIGNATURE"]=>
  string(56) "
Apache Server at domain.com Port 80

"
  ["SERVER_SOFTWARE"]=>
  string(6) "Apache"
  ["UNIQUE_ID"]=>
  string(24) "Ul-ZysC5BCgAABrBpBwAAAVW"
  ["PHP_SELF"]=>
  string(9) "/file.php"
  ["REQUEST_TIME"]=>
  int(1382013386)
  ["argv"]=>
  array(0) {
  }
  ["argc"]=>
  int(0)
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:

domain.com
www.domain.com rewrites to domain.com
test.domain.com
docs.domain.com
help.domain.com
anything-else-at-all.domain.com rewrites to domain.com

The comparison process would consider these URLs

domain.com
test.domain.com
docs.domain.com
help.domain.com
How about this to make it easier:

If Local host
require....
else
require...
<?php if ($_SERVER['SERVER_NAME'] == "localhost")
{
require 'config.php';
}
else
{
require 'home/user/config.php';
}
?>
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.
http://php.net/manual/en/function.include.php
http://php.net/manual/en/ini.core.php#ini.include-path
http://php.net/manual/en/function.set-include-path.php
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:

/home/user/config.php
/home/user/http/files
/home/user/http/subdomains/subdomain-name

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?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.