PHP Includes help


I’ve been trying to work with includes recently and although I understand how they work, I’m still having some trouble.

Currently, I have header.php, footer.php, sidebar.php, and functions.php as my includes, all of which I would like to be included. Where should I place these files and their related images/stylesheets? Do I simply create a directory outside of the public WWW directory so anonymous users cant access them? And if so, however would I then call them using php if they are no longer in the same folder, but instead back a number of folders?


I for example have a sub folder called lib and under that I have another called includes, then in the page I would simply do

include 'lib/includes/';

then I would just have them in order how a normal HTML page would be setup as.

Though you would obviously have it the file structure you have it setup as.

You can put the includes anywhere you like.

But just keep your styles in your main style sheet. Remember that it’s the browser that interprets the CSS styles, and by the time the browser sees your pages, there are no more includes, as they’ve been processed on the server. What’s sent to the browser is just plain old HTML files, with all the various includes placed where they need to be.

You don’t need to, but you can if you want. If they are inside your root folder (say, in an /includes/ folder) you can call them from anywhere with this:

<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/file.php"; ?>

If you want to keep them above the root, do something like this:

<?php include $_SERVER["DOCUMENT_ROOT"] . "/../includes/file.php"; ?>

I put my files I don’t want people to see (like sidebar.php) outside of the public_html so noone can access them but me. Just preference. I just hate knowing that people COULD view that page somehow.