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

eyeloveotters
15,097 PointsQuestions concerning include('inc/header.php') and include('inc/footer.php');
This is undoubtedly a great feature of PHP and I've already started using this practice in developing websites because it makes it so much easier to edit headers and footers across multiple pages in a site.
However, I'm running into a serious problem. When developing the structure of a website, the pages are often nested in subfolders, and subfolders in subfolders. When doing this, all of the links contained in the header and footer would be invalid because they don't contain references to the root directory, such as "../../index.php".
Does anyone know how to fix this problem? Is there a way to make sure the links are always in the proper directory when "including" headers and footers?
3 Answers

Chris Dziewa
17,781 PointsIf you keep going in the php track, Randy talks about creating a config.php file in the includes folder for creating constants. Constants are like variables that you set once and do not change. Generally this information would be kept in there as to stick with the don't repeat yourself model (DRY). Constants are set up by using this:
define( 'CONSTANT_IN_CAPS', '/folderlocation/' ); //folder location is based on your sites location in the server htdocs folder. If it is not in subfolder then you can just use '/'. Capital letters should be used for the name.
Inside the config.php file you can place this:
<?php
define('BASE_URL', '/folder/');
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT' . '/folder/' );
To use this now, go back into your files and include the path to your config file on the top of any page you want to use it for for any includes on that page. To use it for includes type this:
<?php
include(ROOT_PATH . 'inc/filename' );
?>
Next replace any image links and relative links to use the BASE_URL constant. for instance if there is a picture called image.jpg located in an img folder in the main htdocs location, change the source from <img src="img/image.jpg">
to this:
<img src="<?php echo BASE_URL; ?>img/image.jpg">
The reason for doing this is so that if you ever have to migrate your site to a different folder or to put it online, all you will need to change is the config file and all your relative paths will work the same. Be sure to continue on in the php course to see this in more detail and maybe even do a google search on php constants. Best of luck!

olivierandriessen
6,410 PointsYou can include from the root, you can find out what you root is by using $_SERVER['DOCUMENT_ROOT']; Here's an example:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/common/header.php";
include_once($path);
?>

Dylan Grey Aksomitas
Courses Plus Student 15,083 PointsGreat answer Chris, thank you.
eyeloveotters
15,097 Pointseyeloveotters
15,097 PointsAmazing answer. Thank you for taking the time to explain this.
I'm very excited to check out the rest of the PHP track.