How do I include other files in PHP?

55
January 13, 2022, at 11:10 AM

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?

Answer 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

Answer 2

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。

READ ALSO
What causes CSS, HTML, javascript, or React to combine margins?

What causes CSS, HTML, javascript, or React to combine margins?

I have an element with a margin at the bottom of 10pxAnd immediately under this element, I place another element with margin at the top of 10px

43
Show .bmp file in ImageView Android 6

Show .bmp file in ImageView Android 6

My application receives ByteArray withbmp file in it from backend

37
utilize jQuery contextMenu with D3.js forced graph

utilize jQuery contextMenu with D3.js forced graph

I am trying to combine jQuery contextMenu (2x) with a simple D3

37
AssertionError: Must have equivalent tags in image_2, label_2 and velodyne dirs, check those files what is the meaning of it and how to solve?

AssertionError: Must have equivalent tags in image_2, label_2 and velodyne dirs, check those files what is the meaning of it and how to solve?

I was following the official Voxelnet repo on [Voxelnet][1] [1]: https://githubcom/gkadusumilli/Voxelnet/ As per the instructions, I preprocessed the data

62