-2

If i have a file at mysite.com/pages/page1.php and a file header.php, which is in the home directory (mysite.com/header.php), what's the way to include header.php?

I tried include($_SERVER['DOCUMENT_ROOT'] . "/header.php"); or "../header.php");

That doesn't work because nothing that is included or referenced inside header.php works. For example include("meta.php"); doesn't work. Neither does include($_SERVER['DOCUMENT_ROOT'] . "/meta.php");

Is there another way to call header.php that doesn't "move" it to the directory of the file calling it?

ioan
  • 295
  • 2
  • 7
  • 23
  • from `mysite.com/pages/page1.php` home would be `../../header.php` –  May 09 '18 at 22:34
  • Enable error reporting, an include doesn't silently fail. Use realpath() and file_exists() to debug. – Devon Bessemer May 09 '18 at 22:42
  • it just says "Failed to load resource: the server responded with a status of 404 (Not Found)". I can get to header.php, but nothing that it references because it thinks header.php is now in the pages folder. At least I think that's why. – ioan May 09 '18 at 22:54
  • `DOCUMENT_ROOT` will give you the absolute path. Check where its pointing in your server. `echo $_SERVER['DOCUMENT_ROOT']` and also you can append the filename after `DOCUMENT_ROOT` and check if its pointing to the real location and file. – Bee May 09 '18 at 23:00
  • hmm I have a css referenced in header.php, and looks like it's trying to find it at "https: //mysite.com/home/myname/mysite.com/css/styles.css". that doesn't seem right. But either way, I shouldn't have to modify anything in header.php because I know everything it calls works when it's called from the home directory. And header.php is in the home directory. – ioan May 09 '18 at 23:53

1 Answers1

1

PHP provides at least two "working paths" for your files :

  • The function getcwd(), which returns the current working directory (it explains its name),
  • The dirname(__FILE__), which returns the current script's path.

You can find more informations here about getcwd :

According to the documentation of the include statement ( https://secure.php.net/manual/en/function.include.php ) :

The include statement includes and evaluates the specified file.

[...]

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

According to the doc, include will use the script's directory, which is in your case your current working directory (cwd).


The situation

Now, you have the following situation :

  • A file at /meta.php,
  • A file at /header.php, which needs to include /meta.php,
  • A file at /pages/page1.php which needs to include /header.php.

The question is : where is your working directory ? If you run the script /pages/page1.php, your working directory is /pages/.

So, to access /header.php, you must use this statement inside /pages/page1.php :

include '../../header.php';

Or :

include $_SERVER['DOCUMENT_ROOT'] . '/header.php';

These statements are equivalents due to the path of /header.php.

In /header.php, the following statement will fail :

include 'meta.php';

Why ? Because your current working directory is /pages/, so include 'meta.php'; refers to include '/pages/meta.php'; which do not exist, resulting in an error. Remember that the include statement tries to use the current working directory if the URL is relative.


A possible solution

To access to the /meta.php file from the /header.php, you can either :

  • Use include '../../meta.php'; in /header.php if you are calling your script from the /pages/page1.php file,
  • Use include dirname(__FILE__) . '/meta.php'; in /header.php,
  • Use include $_SERVER['DOCUMENT_ROOT'] . '/meta.php'; in header.php.

In your case, $_SERVER['DOCUMENT_ROOT'] is probably the same that dirname(__FILE__), because /header.php is at document root path. However, the former statement is only valid if header.php path is document root. The later is better because it only depends from the path of the current file.


Which one to use ?

The second solution is probably better, because for instance the following statement in /pages/subfolder/page.php will fail :

include '../../../header.php';

The /header.php file will be properly included, but if it contains the include '../../meta.php'; statement, it will refers to include '/pages/meta.php';, because your working subdirectory is /pages/subfolder/.

It is a good design to use the dirname(__FILE__) alternative when you want to include a script relatively from the current file, indepently from the current working directory. Just do not forget the final / because dirname gives the folder of the current script without it.


In conclusion

In /pages/page1.php, use :

include '../../header.php';

In /header.php, use :

include dirname(__FILE__) . '/meta.php';
Fnifni
  • 319
  • 1
  • 10
  • Thanks for the response :) I think i'm getting into meta.php now, but there are a bunch of links like "" in it that would also need something like "dirname(__FILE__)". But those are not in php anymore. – ioan May 10 '18 at 01:16
  • i looked into getcwd() and found that apparently you can change the current working directory with chdir(). Seems like that's exactly what I need, if i can just put it at the top of pages that are not in mysite.com home directory. But it didn't seem to work. – ioan May 10 '18 at 01:18
  • it would be bad to have to edit the links in every file that may be included in page1.php since some of them might have hundreds of links in them. – ioan May 10 '18 at 01:20
  • I think there's an html tag which can help you : https://www.w3schools.com/tags/tag_base.asp – Fnifni May 10 '18 at 03:16