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!
Great post Peter. Dom actually told me about this project that he wanted to do and I honestly told him to talk to you about it. 15min later he’s all jazzed about how smart you are, and shortly after that he was up and running. It makes sense to have one setting and be able to update all your pages at once. Save a lot of time!
Thanks Jean!
Using PHP for areas of the site that are replicable makes a lot of sense–that’s actually some of what a CMS helps to facilitate. However, PHP integrates well with existing sites so it might be easier for someone to get started with just blending HTML and PHP and moving into a framework, like WordPress, later.
I always knew that using php in my header and footer would help me to update those areas without worrying about missing a file update somewhere. It is truly like learning a new language. I buckled down and decided to go through it and it is so much better now. I can’t wait to start the next web project.
PHP can definitely be a useful tool. I hope this article helped you out somewhat and I’ll be doing more like it.
This may be an actual solution, but I am sure I am not the only one who is not willing to have their 500+ page website re-indexed by search engines after changing all the pages to PHP extension.
How long would that take, how much business would be lost? This is not practical unless it is very early on in development.
Hi Steven thanks for commenting! Yes I agree, with large websites re-doing your site language from HTML to PHP isn’t going to be an easy task. You have to weight out the cost and the end all benefits.