Search code examples
phppathstylesheet

path of stylesheet not working php


im trying to link the stylesheet but for some reason I go to the page and its as if its not linked to the page (i.e. as if theres no stylesheet). This is the code for the page. Im new to this so i dont know if im giving enough information and nor can i think of anything else to put in. Thanks in advance

<?php
$path = $_SERVER['DOCUMENT_ROOT'];

echo "
<!DOCTYPE HTML>
<HTML>
    <head>
        <meta charset=\"utf-8\">
        <title>Home - Study Success</title>
        <!-- link to Styles -->
        <link rel=\"stylesheet\" href= \"$path/test/styles.css\">
    </head>
";

include_once ("$path/test/wrapper.php");

also if i view the source page, copy and paste the path of the stylesheet in a new tab, the stylesheet opens up but if i click and select open in new tab i get a blank page. Just opened it on firefox, viewed source page and clicked on the path of the stylesheet, this is what i got, i still have no idea how to fix this!?

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /test/.C:/Program Files       (x86)/Apache/htdocs/test/styles.css
on this server.</p>
</body></html>

Solution

  • Oh I see what you mean, don't rely on $_SERVER['DOCUMENT_ROOT']; that will look in the filesystem:

    <?php
    // this can go in a config file that you can include
    define('CSS_PATH', realpath(dirname(__FILE__).'/css'));
    
    echo '
    <!DOCTYPE HTML>
    <HTML>
        <head>
            <meta charset="utf-8">
            <title>Home - Study Success</title>
            <!-- link to Styles -->
            <link rel="stylesheet" href="'.CSS_PATH.'/test/styles.css">
        </head>
    ';
    
    include_once (CSS_PATH."/test/wrapper.php");
    

    Now im not sure why you have style.css and wrapper.php in the same directory but in this example I assume you placed it in a directory called css.

    So you might need to tweak this, but you get the idea...