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.
-
1yep, you can use [include](http://www.php.net/manual/en/function.include.php) or [include_once](http://www.php.net/manual/en/function.include-once.php) to do this – Pete Jul 10 '14 at 15:19
3 Answers
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

- 560
- 9
- 25
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.

- 474
- 6
- 21
-
Is it possible to use php in a html file? Also, will this work if I am using different websites? – adam Jul 10 '14 at 15:44
-
you can mix html in a .php file, but php might not work in a .html, just close and open the php tags some html here etc... – ArtisticPhoenix Jul 10 '14 at 15:45
-
Thanks for the help, I had been trying to use php in a html file and could not for the life of me understand why it was not working. – adam Jul 10 '14 at 15:48
-
No, do not use php in html file, it will not work. If you have HTML file where you would like to include then just change the `.html` extension of the file to `.php` ...done. – Shashi Jul 10 '14 at 15:50
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.