Reputation: 23
I am currently building a few one-page websites that use the same footer links. As I would like to be able to update them all at the same time, I was wondering if PHP could be used to achieve this. I was thinking that I could write the links in HTML in an external document and use the include
function to grab the html and output it in each of the footers. I am new to php, so I am sorry if this is a very basic question. Any help is greatly appreciated.
Upvotes: 0
Views: 779
Reputation: 71
In fact, the templates of many PHP applications like wordpress, works as you assume. They use include/require command.
In the web root directory, files may organised like this:
Web Root
├── css
├── footer.php
├── header.php
├── images
├── index.php
├── js
└── list.php
In index.php and list.php, use include "header.php" and include "footer.php" to add header and footer codes. So, now if you change something in footer.php, when you visiting index.php and list.php, you can see the changes.
PS: When should I use require_once vs include?
Upvotes: 0
Reputation: 474
The best way is to create a folder named includes
which is normal convention in php projects then create a page called footer.php
and then include that file in your main file, for example you have file on root folder called index.php and you would like to include footer.php there you just write <?php include("foldername/filename.php");?>
that in this case will be <?php include("includes/footer.php"); ?>
.
As you are new, for further reading you may have a look here.
Upvotes: 0
Reputation: 570
If you're building separate websites and intend to use the same footer for all of them, you could create a sample footer html file and use PHP file_get_contents to display it on every website
Upvotes: 1