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

Problem using Constants to Set an Absolute File Path

Here is my inc/config.php code.

<?php

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

?>

Here is my inc/header.php code.

<html>
<head>
    <?php require_once("config.php"); ?>
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="<?php echo BASE_URL ?>css/style.css" type="text/css">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
    <link rel="shortcut icon" href="/favicon.ico">
</head>
<body>

    <div class="header">

        <div class="wrapper">

            <h1 class="branding-title"><a href="/index.php">Shirts 4 Mike</a></h1>

            <ul class="nav">
                <li class="shirts <?php if ($section == "shirts") { echo "on"; } ?>"><a href="/shirts.php">Shirts</a></li>
                <li class="contact <?php if ($section == "contact") { echo "on"; } ?>"><a href="/contact.php">Contact</a></li>
                <li class="cart"><a target="paypal" href="https://www.paypal.com/cgi-bin/webscr?cmd=_cart&amp;business=Q6NFNPFRBWR8S&amp;display=1">Shopping Cart</a></li>
            </ul>

        </div>

    </div>

    <div id="content">

When I have it written it this way, it will not reference my .css file, but when I remove that php command and replace it with "/", then it works fine. I thought the whole point of setting the constant BASE_URL is so that I can substitute it in for file paths. Why is it not working?

3 Answers

John, where is you css file located in relation to the root folder of the page that is being viewed? If the page being viewed is in a folder called "about" and your css is in a folder called "css" the you need to tell the browser to look for the css at"../css/styles.css".

Also, if you are going to define a Base_URL then you should just assign it the current value of the web sites web address, i.e. "http://www.example.com/". Yes, you will need to change that value between you dev and production environments, but it's a fool proof method for determining the BASE_URL for a PHP project.

If you would like a programmatic way to determine the BASE_URL on the fly try using this piece of script:

if( !defined("BASE_URL") ){
    define("BASE_URL", get_base_url() );
}

function get_base_url(){
      if( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ){
        $requesy_method = "https";
    }else{
        $requesy_method = "http";
    }

    $base_url = $requesy_method .'://' . $_SERVER['HTTP_HOST'];

    if ($sub_folder = rtrim(dirname($_SERVER['SCRIPT_NAME']), ' ')){
        $base_url .= $sub_folder . "/";
    }else{
        $base_url .= "/";
    }
    return $base_url;
}

NOTE: This code should work about 90% of the time, but there maybe cases where it will not work. The if statement at the top is there for that reason. It will allow you to define a BASE_URL constant that can override define("BASE_URL", get_base_url() ); if the get_base_url function fails.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jeff;

I think you need a semi-colon in your PHP code on that line after your BASE_URL variable...

Should be

 <link rel="stylesheet" href="<?php echo BASE_URL; ?>css/style.css" type="text/css">

not

 <link rel="stylesheet" href="<?php echo BASE_URL ?>css/style.css" type="text/css">

Let us know if that solves it.

Ken

You don't need to place a semi-colon after the last statement before the closing PHP tag. It is considered best practices, but it is not required.

Referencing it with ../css/style.css also works fine.

The file path for my header.php file is ht-docs/inc/header.php.

The file path for my config.php file is ht-docs/inc/config.php.

The file path for my CSS is ht-docs/css/style.css.

This is why referencing the CSS with /css/style.css or ../css/style.css works. Each of those takes it back to the root ht-docs page. For some reason, the only thing that doesn't work is using BASE_URL to replace the slash in the file path referencing the header. Is there something wrong with config.php, where I set the constants?

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