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

Andrew Merrick
Andrew Merrick
20,151 Points

Code Challenge: Server Paths and Constants 2/3

I've tried every combination to make this work but to no avail. Any suggestions?

<?php 

include("../../config/company.php");

<html>
<head>
     <title>Leadership | Shirts 4 Mike</title>
</head>
<body>

    <h1>Leadership</h1>
    <p><strong>Owner:</strong> ????</p>
    <p><a href="/contact/">Contact</a></p>

</body>
</html>

4 Answers

Kevin Korte
Kevin Korte
28,148 Points

What kind of information are trying to pull in from company.php.

Do you understand what the "../ " means in the url?

That url is saying from this file, go back two folders, look for one called config, and look for a file inside config called company.php

Andrew Merrick
Andrew Merrick
20,151 Points

Well, I now realize that the:

include(../../config/contact.php);

was one folder back too many. I tried:

include(../config/contact.php);

which goes htdoc->config->contact.php but I still don't get the code to run.

The directions say to use a relative server path (not root-relative) so I don't think that:

include($_SERVER["DOCUMENT_ROOT"] . "/config/contact.php");

would work either (and it doesn't). Getting very frustrated here...

Andrew Merrick
Andrew Merrick
20,151 Points

I've also tried:

include(ROOT_PATH . "/config/contact.php");

but since that was not defined that did not work either.

Kevin Korte
Kevin Korte
28,148 Points

You have to define what ROOT_PATH is in a file, that I call config.php, and that call that file to the page first. See my answer below.

Kevin Korte
Kevin Korte
28,148 Points

A big part of your answer depends on how you have your folder structure set up. Here is how I always do it.

In the root folder of my web project, I have an "inc" folder. If I only had one project, the root would be my htdocs folder, but I have more than one project going at any one time.

Inside my "inc" folder I have a config.php file.

For example, lets say my web project was called "kevin". My folder structure would look like this:

htdocs/

kevin/

inc/

config.php header.php footer.php index.php style.css

In my config.php file, I would have this bit of code:

<?php

define("BASE_URL", "/kevin/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/kevin/");

Now with that set, on a different page, I could use ROOT_PATH as a way for php to generate the root of my "kevin" project.

So I could write something like

include(ROOT_PATH . "inc/header.php");

and it would fetch my header.php no problem.

So for you, if you get your config file set up properly, and than call it onto the page as the very first thing, and had a config folder inside of your project's root folder, with a file of contact.php, you should be able to use

include(ROOT_PATH . "config/contact.php");

Does that help?

Andrew Merrick
Andrew Merrick
20,151 Points

Well, let's clear something up... this is not for my own website but for the Code Challenge in Server Paths and Constants.

Since I'm trying to include a config.php I'm going to assume that the BASE_URL and ROOT_PATH constants are already defined in the config file and I'm just including this file into this particular page.

I also tried:

<?php
define("BASE_URL","/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/");
include(ROOT_PATH . "/config/company.php);

But that didn't work either. I don't think I'll need to use BASE_URL since a homepage was not listed in any of the directions so far.

Andrew Merrick
Andrew Merrick
20,151 Points

Correction:

include(ROOT_PATH . "/config/company.php");
Kevin Korte
Kevin Korte
28,148 Points

Do you have a link to the code challenge? I know I did it already, just can't remember the specifics.

Kevin Korte
Kevin Korte
28,148 Points

Oh okay.

Forget everything I just said. I see it now. It is asking for a root relative URL.

Remember that a leading "/" tells us to start at the root. So

"/contact/" 

would get you there, as the code is saying go to the root, and than look in the contact folder.

It looks like you had it right the first time, just get rid of the include at the top.

Andrew Merrick
Andrew Merrick
20,151 Points

Right... thanks... I'm now on part 2 and can't get past it:

There is a file located at htdocs/config/company.php on the server. We need to include that file to get access to some of information about the company. At the very top of the file below, before any of the HTML, include that company.php file using a relative server path.

hence my original post.

Kevin Korte
Kevin Korte
28,148 Points

Umm...so we just went full to circle to come back and find out that you forgot to close your PHP statement on your include in your original answer you posted. lol

If you close the statement, it will past, just copy and pasted your code and it worked. Sorry about all of this.

Andrew Merrick
Andrew Merrick
20,151 Points

Ah, ya know I originally closed it at the end of the HTML instead of at the end of the include. It works. Thanks a lot!