PHP Include from Root with a domain and subdomain

If you think that title is a little confusing, wait until you read the rest of this. :smiley: I’m going to try my best to break it down. I need some help setting my php include paths to work relative to the root. Now I’ve spent alot of time searching, reading, and playing with some options but I can’t seem to get it to work the way I need it to.

I’ve found:

[php]

<?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/common/header.php"; include_once($path); ?>

[/php]

[php]< ? php require_once $_SERVER[‘DOCUMENT_ROOT’] . ” /common/header.php ” ; ? >[/php]

[php]
// This is the one I am using. I have this code posted at the top of my php documents.

<?php set_include_path( implode( PATH_SEPARATOR, array( $_SERVER['DOCUMENT_ROOT'], get_include_path() ) ) ); ?>[/php]

Each of the above codes work in one way and not the other. In my files I am using the third option.

Here’s the situation…

I have a hosting account with godaddy that allows for multiple sites within the same host. Basically shared hosting.

The main domain has a DOCUMENT_ROOT of /var/chroot/home/content/xx/xxxx/html

Then each additional domain is setup with a SUBDOMAIN_DOCUMENT_ROOT.

In this case the SUBDOMAIN_DOCUMENT_ROOT is /var/chroot/home/content/xx/xxxx/html/xyz_site

Now when I’m building this site in Dreamweaver I’ve setup xampp to point to the main xyz_site folder. So while on my local host DOCUMENT_ROOT works just fine. All relative paths work.

NOW

When I load the files to my godaddy server the relative paths no longer work because the paths are working from the DOCUMENT_ROOT and not the SUBDOMAIN_DOCUMENT_ROOT. If I change the code above code from:

[php]

<?php set_include_path( implode( PATH_SEPARATOR, array( $_SERVER['DOCUMENT_ROOT'], get_include_path() ) ) ); ?>[/php]

to

[php]

<?php set_include_path( implode( PATH_SEPARATOR, array( $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'], get_include_path() ) ) ); ?>[/php]

My site will work on the godaddy server but not on my local host. I’ve tried to find a way to reconfigure my xampp to have a subdirectory so I can match the 2 configurations but so far no luck. What I would like to know is there a way to code this differently? I’m going to have 100 +/- pages once this is done so changing the DOCUMENT_ROOT >> SUBDOMAIN_DOCUMENT_ROOT is going to be a real pain.

I hope this all makes a little bit of sense. Please let me know if you have other questions. Any help would be much appreciated!

Hi there,

If I’ve understood correctly, you basically want a way for your code to work both live and locally without changing the code depending on where it is. If so, try the following:
[php]<?php
$server_child = ($_SERVER[‘HTTP_HOST’] == “localhost”) ? ‘DOCUMENT_ROOT’ : ‘SUBDOMAIN_DOCUMENT_ROOT’;
set_include_path( implode( PATH_SEPARATOR, array(
$_SERVER[$server_child],
get_include_path()
) ) );
?>
[/php]

SMOKEY YOU ARE AWESOME!!!

Thanks again for the help.

Haha, my pleasure.

Happy coding!

Another quick way would be:

[php]<?php include($_SERVER[(($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) ? 'SUBDOMAIN_DOCUMENT_ROOT' : 'DOCUMENT_ROOT')] . '/common/header.php'); ?>[/php]

Instead of trying to trick your web server, there is a special variable in the PHP engine called include_path that is capable of holding multiple path strings. You want to add additional strings to this variable to make everything work right.

Speaking specifically for GoDaddy, I think the main domain has a php.ini or php5.ini file in the root which overrides settings for any php.ini files you might place elsewhere in subdomain directories. Thus, if you modify it at the root, that might be good enough.

The dot represents the current path, and the semicolon separates the additional paths. Just adding the dot might be what you need to make things work.

include_path = ".:/usr/share/php:/apps/php"

There’s also ways to do it via a line of code in php, too.

For example add following to your own php script:
[php]<? ini_set('include_path',ini_get('include_path').':../includes:.'); ?>[/php]

For what it’s worth, on my GoDaddy server, I was actually able to create a php.ini file

It has this one line it it:

include_path = "."

I uploaded it to my subdomain folder and everything started working the same as it does on my localhost virtual site root

(FWIW, I also created an identical php5.ini to be safe, as I’m not really sure which one matters to GoDaddy)

Sponsor our Newsletter | Privacy Policy | Terms of Service