PHP Includes - Problem?

chrischris

New Member
Hi

I'm using PHP includes but I have a question about link references.
Do I have to change the reference manually for every new page I make? Or can I have a line of code which works for all pages, regardless of which folder they are in?

I understand about absolute, doc relative and root relative references but nothing I try seems to work...If I try a root relative on one page which works it doesn't seem to work for a page at another folder level in the directory.

<?php include 'includes/header.php'?>
<?php include '/includes/header.php'?>
<?php include './includes/header.php'?>
<?php include '/sitename/includes/header.php'?>
<?php include '../includes/header.php'?>
<?php include '../../includes/header.php'?>

Any help here guys? Bit confused - if the case is that every page's code needs to be different then how can I maintain a large database driven site easily?

Thanks

Chris
 

MelisaMemo

New Member
You can use a line of code which works for all pages.I am close to your answer will get back to you on this after confirming the code.
 

chrischris

New Member
Hey!

Thanks for your reply.

I found this answer somewhere! Because PHP doesn't understand the root relative links you have to use this: (although there are other ways).

This is because the root of the PHP server is different to the document's root.


<?php include($_SERVER['DOCUMENT_ROOT'] . 'root relative link here'); ?>

Thanks!

Chris
 

MarkR

New Member
This is a common issue and there is a relatively easy we to resolve it. One way is to create a php file in your website's root directory (we'll call it loader.php) and put something like:

PHP:
define( 'ROOT_PATH', dirname(__FILE__) . '/' );

Then at the top of all your first layer php files you can call:

PHP:
require_once('loader.php');

or if it's in a sub directory:

PHP:
require_once(dirname(dirname(__FILE__)) . '/loader.php');

You can now call every file from an absolute path relative to your root directory such as:

PHP:
require_once(ROOT_PATH.'include/include_me.php');
 
Top