Fixing Url Issues when Using PHP include()

One of the best features of PHP is its ability to use the built-in include() function to add snippets of code to your web page. The function works great when you're writing out the relative paths, but you start to notice obvious limitations. Specifically the need to constantly adjust the path to navigate out of folders and subfolders.

The most common way to fix this problem is to create a config.php file. This file will define two variables for you to use throughout your code, to keep the relative file paths consistent. We can do this pretty easily by adding the following code to the newly created file:

This assumes that we are working in the root directory on our server, not a subfolder. Otherwise replace / with /your-sub-folder-name/. Note the trailing slash at the end.

That's pretty much it. Now just include the config.php file in your page using the require_once() function and you will have access to the BASE_URL and ROOT_PATH variables. You will most likely need to use the built in $_SERVER["DOCUMENT_ROOT"] variable to build the full file path in order for it to properly resolve when using it inside of subfolders. Here is an example:

Don't forget the?> to close your tag. We did not need to add it in the config.php file since there will never be any HTML in it that the server would need to render. Adding the $_SERVER["DOCUMENT_ROOT"] to the beginning of the path gives us a clean and consistent file path that we can use from any subfolder without having to move up with ../.

How to Use the Variables

Now we should be able to include PHP files using the same relative paths inside any folder or subfolder we want without worrying if they will resolve.

We use the ROOT_PATH variable inside of php functions that require a relative path to a file. The most common one we will use is the include() function. For example, let's say we want to add our header template to the page we're working on. We would first add the require_once() function we defined above, and then below, once the config.php file has been loaded, we write: include(ROOT_PATH . "inc/header.php");.

BASE_URL is used for HTML relative paths. We use this variable when we want to link a stylesheet or a javascript file. It should also be used to add an image or create internal links especially inside files that you plan on including, like a header and footer.

Web Server vs. Local Server

Chances are, you're not developing on your web server. You're probably using a local server and then pushing any changes to your live site via. This is where you may run into some problems. Your file structures can be different between your local and your web servers. For example, I use MAMP locally and use subfolders for projects inside the htdocs folder. My live server has a similar setup and uses a folder named public_html as the web root. Having different folder names should not be a major issue, as we use / to represent the root anyway. The actual issue is the subfolder structure. Let's say my website is saved inside the actual root folder on my web host, not in a subfolder. But locally I use a subfolder to store all the files for that same project. Immediately we see an issue. In the code above, / would no longer resolve on my local server, as the actual path should be something along the lines of /project-directory/. So, how do we fix it?

We can write a pair of if() statements that check if we're working locally and change the path accordingly. Let's assume our local server name is localhost. Our new require_once() function should look like this:

To explain what we're doing, we're asking if SERVER_NAME of our server is localhost. If that is true, we use the local path to the config.php file, else we use the web hosts.

Now we need to add a similar check to the config file, as the BASE_URL and ROOT_PATH variables should be different depending on the environment. The result should be something like this:

This is the same check we used earlier but defines the two variables based on the folder structure.

With these changes made to our files, we can simply reuse the same if statement at the beginning of our pages to utilize our dynamic paths.

Want to download the source code? View the project files on GitHub

Need Help?

No problem. All this can be difficult to accomplish, especially while you're busy with day to day business. That's where Dozen Pixels comes in. We can take care of all your marketing needs. From managing your Social Media presence to creating an effective advertising campaign, we are here to help. Our services range from web design to marketing and consulting. Not sure where to begin? Get in touch and we can discuss your goals and business needs.

Let's Chat.

Each client is unique, and so prices and solutions vary. No cookie cutter work here, so get in touch with us tell us a bit about your goals and needs, and we will layout a custom plan for you. No commitment, no hassle.