0

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!

8
  • Did you try using include('/header.php'); Commented Aug 27, 2013 at 19:58
  • include with a filename is relative to the working directory. the example you give having to work depends on the include path setting. turn it into a filanem starting with ./ or ../ to not make use of the include path and additionally absolutize via __DIR__ magic constant if you like it more expressive to the file itself, not the working directory. Commented Aug 27, 2013 at 20:00
  • @fred that's probably not going to work, because chances are / is not the root of the site but some obscure PHP folder. Rather, either find out the server root and use an absolute path from that, or keep it relative and use include('../header.php'); on pages under a directory. Commented Aug 27, 2013 at 20:02
  • And log warnings to file, so you can see in the PHP error log what the actual error message is: How to get useful error messages in PHP? - PHP normally tells you what the issue is, you only need to "listen" to it :) Commented Aug 27, 2013 at 20:02
  • @LS97 You have a point there. Commented Aug 27, 2013 at 20:03

3 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

This brings up two error messages: Warning: include(/home/gameshan/public_html/reviews/header.php): failed to open stream: No such file or directory in /home/gameshan/public_html/reviews/index.php on line 5 Warning: include(): Failed opening '/home/gameshan/public_html/reviews/header.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/gameshan/public_html/reviews/index.php on line 5
Well in which directory is the file header.php and in which file is the include command? In my example code both are in the same directory (as written in the first sentence). You need to adopt that to your case, so if you tell both filenames this should be easy to solve. Info: You see two errors, but they both belong together, so this is one issue. If you fix it, both errors go away at once.
0

Does this work?

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

1 Comment

this is more a comment than an answer
-1

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.

Comments

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.