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.
ContentBefore 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
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.
<?php
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
<?php
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.
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 subdomain.example.com) inside the main root(folder of example.com), 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.
<?php
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.
Thanks.