0
Root
/index.php ----//The homepage, including head.php works fine
/s/affiliate.php ----//A sub page, will not load the head.php
/structure/head.php

So the above is how its set out. I am trying to include head in pages across my website. I have hundreds of pages. It works fine on the index.php using

<?php include "structure/head.php"; ?>

However on any sub pages, it just doesnt load. I guess its the way it searches through directories but I have also tried to make it search from root and it still isnt working, any ideas?

 <?php include( $_SERVER['DOCUMENT_ROOT'] . '/structure/head.php' ); ?>
Miguel G. Flores
  • 802
  • 7
  • 21
  • Show your `DOCUMENT_ROOT` value – maximkou Jun 03 '16 at 16:08
  • Just for future this called debugging. You might start with simple things like `var_dump($_SERVER['DOCUMENT_ROOT'] . '/structure/head.php')` And work your way up to xdebug or something. Very useful skill ;) – E_p Jun 03 '16 at 16:10
  • Possible duplicate of [Are PHP include paths relative to the file or the calling code?](http://stackoverflow.com/questions/7378814/are-php-include-paths-relative-to-the-file-or-the-calling-code) – HPierce Jun 03 '16 at 16:27
  • Do you want to **always include the file relative to the current PHP script**? If so the just do: `include __DIR__ .'/'. "structure/head.php";` It will always include that file that is in the same directory as the script. If it was in the parent directory then include a relative directory path. i.e. `include __DIR__ .'/../'. "structure/head.php";` etc. – Ryan Vincent Jun 03 '16 at 16:48

3 Answers3

1

use relative paths:

include("../structure/head.php");
SparK
  • 5,181
  • 2
  • 23
  • 32
  • Was hoping there was a simpler way of doing it which could remain the same across all pages – Matt Dempsey Jun 03 '16 at 16:17
  • If your subpage was a controller action invoked from your index using a router and loading a template with the include inside, then all paths would be relative to index. But architecture is off-topic. – SparK Jun 03 '16 at 16:20
  • may I suggest that _absolute paths are always used_ i.e. `include(__DIR__ .'/'. "../structure/head.php");` That way, as the `current working directory' is never used then the file that is relative to the current script will always be included. i.e imo, never use relative paths in PHP scripts. _Always use absolute paths with a relative directory as part of the path_. – Ryan Vincent Jun 03 '16 at 16:33
  • @RyanVincent I agree with absolute paths, but `__DIR__.'/'."../"` is not an absolute path. It's a realive path using the current directory of the file as base and not the current working directory of php (which may vary) – SparK Jun 03 '16 at 23:20
  • No, it is an absolute path - **it does not use the Curremt Working Directory**! It has relative directories in it. **It is still an absolute path according to the operating system**. This is not a PHP thing. This is an O/S thing. `__DIR__` is **always the full path to this PHP script**! – Ryan Vincent Jun 03 '16 at 23:23
  • I can provide code that shows this if you want? Look- check the [realpath — Returns canonicalized absolute pathname](http://php.net/manual/en/function.realpath.php). Try it in scripts and change the `CWD` with `chdir` -. Using relative paths - it returns different results. Using absolute paths with relative directories it returns the paths that you expect. – Ryan Vincent Jun 03 '16 at 23:35
0

Try to use

$_SERVER['HTTP_HOST'] 

instead of DOCUMENT_ROOT,

EDIT

This should work

$protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
$web_root = $protocol . $_SERVER['HTTP_HOST'];
define('WEB_ROOT', $web_root);

include (WEB_ROOT.'/file.php');
DeChadou
  • 31
  • 3
  • Unfortunately this did not work – Matt Dempsey Jun 03 '16 at 16:15
  • Besides __DIR__ i think the best option is to create a config file and define your absolute path there. You can use this absolute path whenever you want later $protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://'; $web_root= $protocol . $_SERVER['HTTP_HOST']; define('WEB_ROOT', $web_root) – DeChadou Jun 03 '16 at 16:20
0

Use include("../path/to/head.php"); This should work since it will be able to direct your code to the sub directory where you have stored your head.php