3

Can anyone help me use the correct way of doing includes. Rather than: include('header.php');

It's a multi page site and i want to add files into other folders for example an Information folder.

In this information folder i have a file called aboutus.php which has includes to the header in the previous directory.

I thought about using the $_SERVER['DOCUMENT_ROOT'] method, but it seems a bit bulky and my site is in a sub folder from the root. So I would have to manually hard code the sub directory. Is there an easier way?

adamdehaven
  • 5,890
  • 10
  • 61
  • 84
user2183216
  • 359
  • 3
  • 9
  • 22

3 Answers3

6

This can be a bit confusing because $_SERVER['DOCUMENT_ROOT'] is set by PHP based on the web server, the Web Root is set by the web server (Apache or IIS), and the URL uses a similar notation to the two when delivering and accessing files in browsers. If the web root is above the domain name in a folder it can be confusing if you're using a single / to access the root directory in HTML.

I'm not going to get into server config files for these, only PHP and URLs in client-side code.

PHP Includes

Because [web] hosts can change your directory structure by moving your web directory to a new server (think shared hosting), or because you may need to use a development server in a different environment than the production server, the worst method when linking to files is to use the full "hard-coded" path to the file.

//Example of WORST METHOD
include('/usr/local/etc/www/html/includes/config.php');

As long as all of your files are above the document root or will remain in the same general location on a different server you *can* include() with previous directory notation. When you're setting an include() or require() with dot notation the dots are relative to the file using the notation, so it can become a bit trickier to follow.

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

If inside of the header you are including a "config.php" in the previous directory, it would say:

include('../config.php');

In actuality the real file paths could be:

$_SERVER['DOCUMENT_ROOT'].'/some_folder/header.php';
$_SERVER['DOCUMENT_ROOT'].'/config.php';

Best practice

Keep the includes INTUITIVELY all in one place. If header is in the "includes" folder just inside of the document root you can do something like this:

$root = $_SERVER['DOCUMENT_ROOT'];
//folder for functions, headers and so forth
$includes = $root.'/includes/';
//For database passwords and other code that should
//not be above the document root.
$safe = $root.'/../safe/';

require_once($safe.'db_connect.php');
require_once($includes.'config.php');
require_once($includes.'header.php');

Anytime you would need to include some other file you would only use the $includes var.

Another problem with previous directory notation is when you're making nested directories and including the same file it becomes a bit difficult to move a folder or follow the code.

I'm always editing sites that have code like:

include ('../../../shell.php');

Not a problem with a shell page... but if you have multiple configs (I've also run into this). Working with more than one developer this can get out of hand because all files open in a text editor say config.php and something will eventually be overwritten.

//Site Config
include('../../config.php');

//Project Config
include('../config.php');

Link Paths and URLs in client-side code

There is a difference between URLs and File Paths. When you're accessing files on a local machine with PHP it's best to use file paths.

$path = $_SERVER['DOCUMENT_ROOT'].'/includes/header.php'; //or
$path = '../includes/header.php';

/*or if you're on Windows*/
$path = $_SERVER['DOCUMENT_ROOT'].'\includes\header.php\;

When you're using a client-side language like HTML, CSS, or Javascript you want to use URLs. A URL can be also accessed without writing out the full URL above root and it looks similar:

$link = 'http://www.domain.com/css/site.css'; /*or*/
$link = '/css/site.css';

Document root in HTML, CSS, and Javascript is simply a / character.

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
  • This issue is that in my header.php which is in the directory before has css and images. As a result it messes up the location of them as im not in that directory – user2183216 Apr 23 '13 at 16:06
  • 1
    `include()` is server side, if you're running into issues with images and CSS that is more the way that you're accessing the images with client-side code like CSS, HTML, or Javascript. – AbsoluteƵERØ Apr 23 '13 at 16:12
  • I wanted to use a config to declare the site url, but because the include is in the header.php it doesnt pull this config in. My about us page is in a directory call information: My header then contains the config, which is in the directory before: include('config.php');?> – user2183216 Apr 23 '13 at 16:16
  • You should read this answer, @user2183216. Then ask a follow-up question that relates to something the answer says. Right now your follow-up questions seem unrelated to the answer's content and unrelated to the author's response. – Lightness Races in Orbit Apr 24 '13 at 09:02
1

Please add below line of code in the header:

<?php $root= $_SERVER['DOCUMENT_ROOT']."../../../../";?>

Then you can use $root variable as a site path for displaying any images on your site,

Example

If you store images in dir (www.websit.com/data/images), then you can get data with below line of code.

<img src="<?php echo $root;?>data/images" alt="your alt"/>

Thanks!

Chandresh M
  • 3,808
  • 1
  • 24
  • 48
0

Shortly, add a "persistent subst" as "W:" drive to C:\Inetpub\wwwroot. Then set W: as "doc_root" and add it to "include_path" within php.ini

See my full answer at root path doesn't work with php include. SUBSTing to a drive also helps debugging a client-side-only code.

Community
  • 1
  • 1