Austin Bunker
Austin Bunker

Reputation: 53

PHP header file isn't working in a subfolder

I have a reviews page on my website, and I wanted my website to be extremely user friendly, so I made it a sub-index. I have my index.php under a folder named reviews (found here) so the domain is just /reviews. When I try to include a PHP or CSS it abandons them and excludes them.

The code I am using to include my CSS (which is working on every other page) is:

<link rel="stylesheet" type="text/css" href="/stylesheets/index.css">

The PHP include() that I'm using is:

<?php 
    include('header.php');
?>

This PHP works on all pages that do not use a parent folder, ex. index.php (for my homepage).

The HTML in the PHP document is:

<html>
<center><nav>
<ul>
    <li><a href="/">Home</a></li>
    <li><a href="">Arcade</a>
        <ul>
            <li><a href="/arcade/action">Action</a></li>
            <li><a href="/arcade/arcade">Arcade</a></li>
            <li><a href="/arcade/puzzle">Puzzle</a></li>
            <li><a href="/arcade/vehicle">Vehicle</a></li>
            <li><a href="/arcade/violence">Violence</a></li>
            <li><a href="/arcade/defense">Defense</a></li>
            <li><a href="/arcade/rpg">RPG</a></li>
        </ul>
    </li>
    <li><a href="">Watch</a>
        <ul>
            <li><a href="/watch/tv">TV Shows</a></li>
            <li><a href="/watch/movies">Movies</a></li>
        </ul>
    </li>
    <li><a href="">Extras</a>
        <ul>

            <li><a href="/news">Updates</a></li>
        </ul>
    </li>
    <li><a href="/support">Support</a></li>
</ul>
</nav></center>
</html>

Anybody know any solutions to get my PHP and my CSS working in sub-folders?

My website is http://www.gameshank.com/

The homepage is using the header.php file!

Upvotes: 0

Views: 3804

Answers (3)

Austin Bunker
Austin Bunker

Reputation: 53

Call the .PHP file by:

<?php
    include('../header.php')
?>

The /../header.php won't work either...

Then clear your browser history and cookies, then it works from the editing end of things.

Strange fix, but it worked. But until the editor was cleared of history and cookies, it wouldn't work.

Upvotes: -1

hakre
hakre

Reputation: 197659

If you know that header.php is in the same directory as from the file you want to include it to:

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

This is immune to changes in the working directory and therefore pretty fail-safe.

You might find as well the following Q&A interesting:

Also it's important to understand the difference between the CSS-URL (URL-path is resolved in the browser) and the paths in PHP (those file-paths are resolved on the server, the browser is yet far far away).

Upvotes: 4

Wim Molenberghs
Wim Molenberghs

Reputation: 902

Does this work?

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/header.php";
include($path);

Upvotes: 0

Related Questions