Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Cleaning URLs with Subfolders Introducing Constants

steven schwerin
steven schwerin
7,722 Points

Base URL

Hi,

I don't understand how the Base_URL constant is different from using the root relative format. Sorry for this newbie question.

Many thanks,

Steve

2 Answers

Hello,

You may be getting confused because we are using these in a local environment and not a production environment. Let me give you an example of these two in a real-world production environment:

  • Given the domain name: www.example.com
  • Given the web server root (the directory where the server files are placed in relation to the server): home/user9876/public_html

Now keep in mind that the server root in this example is based on a shared web server (multiple users on one server) using CPanel, and the user in the server path is the account name (username for account being user9876).

The BASE_URL constant would be set to example.com while ROOT_PATH would be set to $_SERVER['DOCUMENT_ROOT'] which is home/user9876/public_html.

You would use BASE_URL for all of your pages on your site so they would always have www.example.com/{page}. The ROOT_PATH would be used to include files inside of the script like the header, footer, functions, etc. You could use the BASE_URL for including files but it's better to use relative paths in case things change in the future. My rule of thumb on determining which of the two you should use, if something is going to be included in the back-end, use ROOT_PATH and if you are linking to something on the front-end, use the BASE_URL.

In summary BASE_URL is a URL safe constant that you should use for links and anything on the front end and should be set to the base directory of your website while the ROOT_PATH constant should be used when including anything on the backend (except for CSS and JS as the LINK and SCRIPT tags need URL-based pathing) and should be set to the base directory of the web server. Hope this helps.

Cheers!

steven schwerin
steven schwerin
7,722 Points

I am going to read this over a few times. Thank you so much. I am a big context person. This is exactly what was confusing me. The concepts still need to sink in, but I can see what they are now. This really is a great answer.