0

I'm working on using PHP to give myself the same header, navigation and footer elements across all pages, but I have a problem; for some reason the .php file isn't recognizing the header.html file for a include("/header.html")

.php file

    <html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<link rel="stylesheet" type="text/css" href="/style.css">
<title>Sample Game Type</title>

</head>
<body>
    <?php include("/header.html"); ?>
    <div id="gamelisting"><!--Gametype Shortcuts-->
        <a href="http://www.website.ca/game-types/carpe.html">
            <div class="gametype">'Carpe' (also known as just Carpe)</div>
        </a>
        <div class="gametype">Other Game Here</div>
        <div class="gametype">Add more as neccessary</div>
    </div>
    <div class="gameinfo"><!--Information about game type-->
        <h2 style="text-align:center;"></h2><!--Name-->
        <h3>Description</h3>
        
        <h3>Objective</h3>
        
        <h3>Rules</h3>
        
    </div>
</body>

And then my header.html file.

    <html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Header</title>
</head>
<body>      
    <li><!--Header Buttons-->
        <a href="http://www.website.ca">
            <div id="header">
                Home
            </div>
        </a>
        <a href="http://www.website.ca/mods.html">
            <div id="header">
                Mods
            </div>
        </a>
        <a href="http://www.website.ca/selling.html">
            <div id="header">
                Selling
            </div>
        </a>
        <a href="http://www.website.ca/gallery.html">
            <div id="header">
                Gallery
            </div>
        </a>
        <a href="http://www.website.ca/contact.html">
            <div id="header">
                Contact
            </div>
        </a>
    </li>
</body>

What am I doing wrong? The header.html file is in the root of my pubic_html file with the .php page I'm trying to connect with being in directory /game-types

1
  • Just an aside, you probably shouldn't have a html, head tags in the the header.html file since you already have those in your php file Commented Oct 27, 2015 at 0:23

1 Answer 1

3

When you use <?php include("/header.html"); ?> you are really saying look for a file named header.html at the root of my entire filesystem; not in your public_html directory (or even in a predefined include_path) with all your other files, like /path/to/public_html/header.html, you are literally looking for a file at /header.html.

It sounds like you want to include a file relative to the current directory.

Something like this:

include(__DIR__ . '/header.html');

or

include('header.html');
Sign up to request clarification or add additional context in comments.

7 Comments

So would I do better to say <?php include("/public_html/header.html"); ?> If that is, in fact the proper path from the root of my file system? I'm trying to make a single header.html file work for the entire site with multiple directories.
__DIR__ is a constant which will return the current relative directory, so if you are in the php file /path/to/public_html/index.php and using __DIR__ it will return /path/to/public_html
Okay, however my I am in /path/to/public_html/game-types/webpage.php but I also have files in /path/to/public_html/ and other directories that I want to have access to this header file, not only those in the /path/to/public_html/ directory.
Then link relative to those directories. I.e. if I have a file named lol.html directory above my current directory then I need to look at __DIR__ . '/../lol.html' or link via the full path. It's up to you.
@SFN Put a . between _DIR_ and '/public_html/header.html'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.