0

I just started creating a website at my home.

Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.

So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.

Then I did include the header & footer pages inside the index.php page by using these following codes.

<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>

Undoubtedly, they worked fine without any trouble.

Then I created a directory as account inside the htdocs.

Now I've a login.php page inside the account directory (/account/login.php).

Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.

So, how can I include them while the login page is in account directory?

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
S.G.
  • 182
  • 2
  • 12

2 Answers2

1

When creating sub directories and including files it is always simpler to use absolute file paths.

The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.

Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript

Stan Howe
  • 116
  • 2
  • 11
  • With regard to PHP and `include()`, absolute path would be any path starting with `/`, denoting the root of file system (or starting with `c:\` - or any other drive letter - on Windows). You wouldn't usually want to include something through HTTP wrapper as the included script would be interpreted by the server and only the result (HTML, JSON etc.) would be returned. – Jirka Hrazdil Dec 08 '17 at 11:39
  • Yes indeed, hence my comment: 'you can even remove the domain and just have /modules/header.php.' – Stan Howe Dec 08 '17 at 11:42
0

I think it's a file path problem,you can use this code:

<?php include '../header.php'  ?>
<?php include '../footer.php'  ?>

Load the file of the first level directory。

Jack Zhao
  • 16
  • 2