Jump to content

Recommended Posts

Hi there, I have coded a PHP site with mostly HTML so I can utilize include statements for the header and footer and side menus.

 

My issue is this:  I put all of the include files in a folder called includes.  Then some of my files are at the root level and some are one directory lower.  All of the files in different directories work fine with the include statement <?php include('../includes/header.php');?> because the header file is set up to access the various levels of the site "above" the include folder, so for example the header file grabs an image using ../images/whateverpath.

 

When I try to include the header file in index.php (include(includes/header.php)  it doesn't work because the header file is using paths inside the includes folder, if I strip away the ../ from inside the header file, then the index.php file works but then none of the other files in other directories work. 

 

It seems that I would need 2 header files for my site to work, but this isn't very efficient.  How do I make the include files work on all levels of the site regardless of where they reside?

 

If anyone can help me I would be forever grateful as I am very frustrated.

 

Thanks,

 

Link to comment
https://forums.phpfreaks.com/topic/175394-help-with-includes/
Share on other sites

Use absolute path from root:

This might be useful:

<?php

include $_SERVER['DOCUMENT_ROOT']."/lib/sample.lib.php";

?>

So you can move script anywhere in web-project tree without changes.

 

Good luck,

Handy PHP

 

 

Link to comment
https://forums.phpfreaks.com/topic/175394-help-with-includes/#findComment-924249
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.