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!

putvande
  • 15,068
  • 3
  • 34
  • 50
Austin Bunker
  • 53
  • 1
  • 3
  • 13
  • Did you try using `include('/header.php');` – Funk Forty Niner Aug 27 '13 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. – hakre Aug 27 '13 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. – LS97 Aug 27 '13 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?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) - PHP normally tells you what the issue is, you only need to "listen" to it :) – hakre Aug 27 '13 at 20:02
  • @LS97 You have a point there. – Funk Forty Niner Aug 27 '13 at 20:03
  • I have my .PHP file in the main directory, it isn't in any folders, and my stylesheet is in a folder call stylesheets. – Austin Bunker Aug 27 '13 at 20:06
  • Really.. does anybody use `center`? Don't use that. It is also not supported in HTML5. – putvande Aug 27 '13 at 22:22
  • What should I use instead? – Austin Bunker Aug 28 '13 at 10:10

3 Answers3

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

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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 – Austin Bunker Aug 27 '13 at 20:22
  • 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. – hakre Aug 27 '13 at 20:25
0

Does this work?

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/header.php";
include($path);
Wim Molenberghs
  • 882
  • 8
  • 7
-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.

Austin Bunker
  • 53
  • 1
  • 3
  • 13