Add PHP Today: Header and Footer
Even if you aren’t using a content management system, you can still add some of the benefits of PHP to your site today. This will take you through some tips and tricks to make your site cooler and easier to use and manage just by adding a few simple lines of code.
Today we’ll be taking a look at the headers and footers. In these, content is the same throughout the site and having consistency, especially in the navigation bar, can be critical for the user to fully experience the site. When you are adding or changing pages, it can be a hassle to go through and change all the separate HTML files. PHP can help.
To start, you will need to change all of your .html files to .php. This can be done simply by opening them in a text editor and saving as the new file extension.
Next, you will have to locate where the duplication occurs. For SEO purposes, you may not want to have the <title> </title> the same throughout the site, so for the purpose of this how-to, I’d recommend locating your <body> tag and copying from right below to the end of your navigation bar–depending on your site’s design. Open a new file in your text editor and paste the code there. Save as header.php.
Do the same thing for your footer. Generally you can copy from the beginning of the footer code to the </html> tag. Copy into a new file and save as footer.php.
For the rest of your site files, you will need to go through and replace the areas you copied with php code. The PHP function ‘include’ will add the file’s content prior to hitting the browser, so it will appear just like your normal HTML file. Just make sure that you remember to upload the header and footer files, or else the page will appear without these areas.
For the header area:
<?php include ‘header.php’;?>
For the footer area:
<?php include ‘footer.php’;?>
Now if you need to change your header or footer content, you will only need to modify one file, rather than going through each individual .html file, which can be time consuming to say the least.
This is just one way PHP can make your site better. Stay tuned for more!