Hello guys in this article we will learn how to create header and footer on all pages of website by one line of php code. In this post I have written some tips and trick about header and footer, hope you guys find it useful. You like coding, learning to make websites online. Stucked at a point that how to include header and footer files on whole website. At starting when I was learning coding online, I also faced this issue. So I just decided to write a post on it. In this article I have discussed in detailed that you should know before you start creating a website.
Follow these instructions given below to make your basic concept clear:
First thing you need to do is create header.php and footer.php file in the home directory of your website. Keep in mind that for running .php files you need a server, it may be online or offline. For offline you can use software like wamp server. Create your header and footer eye catching so that user will like it.
After that write the code for header at the top of any page where you want header file:
<?php include(‘header.php’); ?>
and for footer similarly write the code as, at the bottom:
<?php include(‘footer.php’); ?>
Note: The above code works well only for those files which are in the main home directory of your website.
If you want header and footer in those pages that are placed in sub-folders of your directory then this code will not work. But wait, here we an another php code that allow us to include the files in the sub-folders. This code is as:
<?php include $_SERVER[“DOCUMENT_ROOT”] . “/header.php”; ?>
<?php include $_SERVER[“DOCUMENT_ROOT”] . “/footer.php”; ?>
Read this code here you will find a slash / symbol before the file name. It is very important, don’t forget to add this symbol before the file name. If you don’t use / before the file name then your file will not be loaded in the desired page that is in sub-folder. So make sure you have used / sign. It is not compulsory that file names must be header.php and footer.php. You can change the file name but must be followed by the .php extension.
Tips for the page where you include the header and footer:
Before including header file on page make sure to add it after the <title> and <meta> tag, because these tags are different for every single page.
Yo don’t need to add <html> <head> <title> <meta> tag in header.php file. But make sure to close </head> tag in header.php file.
You can start <body> tag in header.php file and add your menu code. And in footer file at the ending you can add </body> and </html> tag otherwise you need to write it on every single page. If you want to include sidebar, you can use this method.
So guys this is the basic concept of header and footer I hope you guys find it useful. Thanks for reading and keep visiting on this website.