0

I'm trying to include a block of html with hrefs. What i want to be able to do is only maintain one include file. Current code is:

<?php include __DIR__ . "/../../../includes/footer.php"; ?>

This links fine except any hrefs in the footer.php file must be prefaced by ../../../ due to the location. This means that I would have to create multiple footer files with different hrefs.

To display folder structure(*indicates where includes of the same php file are needed):

ROOT
¦
+---includes
¦   ¦
¦   +---footer.php
¦
¦
+---main
¦   ¦
¦   +---maps
¦       ¦
¦       +---uk
¦           ¦
¦           +---map.php*
¦
+---gallery.php*
¦
+---assets
    ¦
    +---icons
        +
        ¦
        +---facebook_1.png
Scott
  • 63
  • 1
  • 10
  • You could use document root, something like this: `require_once($_SERVER['DOCUMENT_ROOT'] . 'directory/directory/file');` – Denis Solakovic Aug 18 '17 at 08:12
  • Try to use `$_SERVER["DOCUMENT_ROOT"]."/includes/footer.php"` to access your footer file from your `map.php` and `gallery.php` – Richard Aug 18 '17 at 08:13
  • When you say, _“any hrefs in the footer.php file”_, you mean actual HTML links you are outputting there, right? Well the best way to handle that IMHO is to use URLs relative to the domain, starting with a slash. – CBroe Aug 18 '17 at 08:16
  • unfortunately using both DOCUMENT_ROOTs as you have depicted does not include the file at all. & yes i mean html links. eg. and @CBroe that is exactly the behaviour i wish to employ, but it does not give me the image without using ../../../ – Scott Aug 18 '17 at 08:29
  • So where in your folder structure is the `assets` folder located …? – CBroe Aug 18 '17 at 08:31
  • @CBroe updated to include assets – Scott Aug 18 '17 at 08:34
  • @CBroe this is what i would expect to have to use with my limited knowledge, but does not work unless prefaced with ../../.. – Scott Aug 18 '17 at 08:37
  • But your domain root is identical with ROOT in your structure shown above, right? Meaning, `http://yourdomain.tld/` mapes directly to ROOT (and then loads the index file from there, or sth. like that.) – CBroe Aug 18 '17 at 08:40
  • _“unfortunately using both DOCUMENT_ROOTs as you have depicted does not include the file at all”_ - then please verify first of all what document root is actually set to. – CBroe Aug 18 '17 at 08:41
  • @CBroe From MAMP folder looks like: MAMP > htdocs > radventures.co.uk (ROOT depicted in original post) > index.php – Scott Aug 18 '17 at 08:50
  • started looking into mamp conflicts. if i use HTTP_ROOT it links the file, but links are still firing from the location of the original.php doc, not the root. – Scott Aug 18 '17 at 09:06
  • looks like this is part of a more complex issue: https://stackoverflow.com/questions/789816/how-to-set-the-document-root-and-site-root-in-my-local-php-dev-setup – Scott Aug 18 '17 at 10:11
  • i edited my document root in mamp. root is now where i thought it was. Links in included .php file still dont fire from root though! – Scott Aug 18 '17 at 10:21
  • _“root is now where i thought it was”_ - so it wasn’t before? Have you tried `/assets/icons/facebook_1.png` again after making the change? – CBroe Aug 18 '17 at 10:23
  • it appears mamp hijacked the document_root and took it a folder up. i have tr – Scott Aug 18 '17 at 10:29
  • i have tried /assets/icons/facebook_1.png, but this does not point to root. i have also tried using href= " , but this returns /Application/MAMP/htdocs/radventures.co.uk/linkhere.html – Scott Aug 18 '17 at 10:39
  • using href= " gives desired behaviour!! but will this work once hosting for real? – Scott Aug 18 '17 at 10:46

1 Answers1

0

Finally worked it out. Mamp was confusing things and i had to include php in my link.

under MAMP>conf>apache>httpd.conf I changed my DocumentRoot to "/Applications/MAMP/htdocs/radventures.co.uk" so it pointed to the root of my site.

Changeable document_root for mamp configuration:

<?PHP
  if ($_SERVER['DOCUMENT_ROOT'] == '/Applications/MAMP/htdocs/radventures.co.uk'){
    $root = 'HTTP_ROOT';
  }
  else {
    $root = 'DOCUMENT_ROOT';
  }
  $link = "$_SERVER[$root]";
?> 

img link:

<img src="<?PHP echo ($link . "/assets/icons/facebook_1.png"); ?>

include:

<?PHP include ($_SERVER['DOCUMENT_ROOT'] . "/includes/footer.php"); ?>
Scott
  • 63
  • 1
  • 10