Including Files From Root With Sub Domain in PHP

This article will describe you how to include files with php from root when you are using a sub-domain.

When user creates a subdomain in their host, normally the host creates a folder inside the root and make it the root of a subdomain. (This happens usually if the host is a shared one) So, if we need to access files in the root we can't use $_SERVER['DOCUMENT_ROOT'] variable. (the reason will be discussed later). Although we can use absolute paths to do that, it's not a good practice. If you need to change your host, you have to change all paths which will be impossible if you have a big website. Therefore we need an alternative. We introduce a trick to do it with php. We teach you where and when to use it.


Before You Start

Before you start I'd like to say, you must have the basic knowledge on including files and working with included files. But it doesn't matter if you don't have it now. I'm pretty sure that you'll understand including even you are at zero. Feel free to login/signup and comment below.

Next, please consider that the whole tutorial is based on following two paths

root         www/websites/example
subdomain    www/websites/example/subdomain

Basic Including

Here we learn how the basic including works with php. If you know how to include and use included files, skip the step.

We have a really useful php super global which can be used with including. It is $_SERVER['DOCUMENT_ROOT']. Let's learn how to use it with the root domain.

Once you have index.php and header.php in the same directory, you can include header.php in index.php as following.


include_once 'header.php';

Normally, developers save their include files (and class files too) in a directory in the root called inc, include or including_files. What if we need to include those file in a file which is in /ajax subfolder? In that case we need to use $_SERVER['DOCUMENT_ROOT'] variable

Assume that the file we are going to include (header.php) is in /inc/ folder in the root


include_once $_SERVER['DOCUMENT_ROOT'] .  '/inc/header.php';

We can use this trick to include any file, in any file if the both has the same document root value.

Including Files in Sub Domain

Normally, most of shared hosts creates subdomain and link them to a folder in the main root. If you have the subdomain folder outside the main root either you have to move it into the main root and continue the tutorial or use absolute paths to include files.

When you have the subdomain folder(folder of inside the main root(folder of, you can use a trick to include files. Assume that you have index.php in your subdomain and /inc/header.php in the main root. So, you can include files as following.


include_once $_SERVER['DOCUMENT_ROOT'] .  '/../inc/header.php';

Good News is, you can use /../ as much as you need to go back in folders. One goes one folder back.


We discussed how to include files from root with subdomain. It's hard to include many files at the top if you are using classes. So, check our autoloading tutorial to learn more.If you have further questions to ask comment below or be with our twitter. Hope you enjoyed the tutorial.


Tagged: PHP
You can connect with me on Twitter or Linkedin.
Latest on My Blog
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Laravel High CPU Usage Because of File-based Session Storage
Laravel High CPU Usage Because of File-based Session Storage
Image for Resizing Droplets: A Personal Experience
Resizing Droplets: A Personal Experience
Image for Moving our CDN (10+ GB images) to a new server
Moving our CDN (10+ GB images) to a new server
Image for Disqus, the dark commenting system
Disqus, the dark commenting system
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Related Articles