PDA

View Full Version : include paths


zenriot
03-10-2005, 07:13 PM
Hi, I am trying to have the menu of my website included on every page using include_once().

I normally program in ASP, and the PHP syntax for includes is a bit different. I want to be able to have the file included on every page, regardless of how deep it is in the file structure.

I also want to avoid having to include the entire path (ie. c:\entire\path\to\include\file.php) if possible, because this will change when moving from development server to production server.

In other words, is there a way to set the include path relative to root folder of the website, rather than relative to the file that is calling it, or right from the root of the whole file tree?

If I can't do that, and have to make some sort of case select (ie. check the hostname and set the path accordingly), is there a way to set this in only 1 place without running into the same problem of having to set the paths for THAT include?

I am aware that the include_path can be set in the php.ini file, however since the production server isn't mine and hosts many sites, I don't think that is an option.

devinemke
03-10-2005, 07:39 PM
include($_SERVER['DOCUMENT_ROOT'] . '/path/to/menu.php');

Zurke
03-13-2005, 03:32 AM
It was my understanding that php also looks for includes within the directory of the document. It placed within the directory it would find it?

marek_mar
03-13-2005, 11:49 AM
Let's assume you have this file structure:

some/folder/index.php
some/folder/admin/index.php
some/folder/includes/myClass.php
some/folder/includes/myFunctions.php

Your application is located in the folder "folder". This is your apps root. You want to include all files in the includes folder to both index.php files.
Your /index.php looks like:

<?
$my_root = './';
include $my_root . 'includes/myClass.php';
include $my_root . 'includes/myFunctions.php';
// some code
?>

The admin/index.php would be:

<?
$my_root = '../';
include $my_root . 'includes/myClass.php';
include $my_root . 'includes/myFunctions.php';
// some code
?>

When the some/folder/index.php file is called all relative includes will strat from some/folder/ even if you include a file from from another folder.
If you would like to use absolute paths you can convert these relative ones using realpath() (http://www.php.net/realpath)