|Need help with PHP Includes please|
Our website has expanded a lot since we started it and now I've been asked to add on more pages. This means updating the navigation on every. single. page. And obviously, I don't want to have to keep doing this. My only thought is to use PHP includes, however as I have never used PHP before, except for our e-mail forum, I'm not quite sure what to do.
I understand the whole creating a new PHP page just for the navigation and linking it to the other pages, however, will I have to change all of the website pages to .php? At the minute they are .html / .htm.
If I change them all, do I have to remove the .html / .htm ones from my hosting server and replace the pages with the .php ones?
Will having every page as .php affect the website?
Will it make it quicker to load or slower?
How will Google know that the pages have been changed?
Will it affect our Google rankings?
Yes, I'm confused. Your help is much appreciated!
With regard to the replacing .html with .php, you can keep the same extensions for your pages by using this command in an .htaccess file
AddType application/x-httpd-php .htm .html
This command instructs your web server to execute php code inside web pages with .html extensions.
Inside your .html web page you can then have code like this
which will be executed.
The nav.php file can contain html code for your nav menus.
Then for any future changes, you simply change nav.php once and all your page that include nav.php change as well.
There will be a slight slowing down of page load, but I think it would be negligible.
Google hopefully will be crawling your pages on a regular basis and will pick up on any additions to your navigation, regardless of whether you add them manually to each page or use the nav.php include.
Again using the hard-coded nav or nav.php include should not affect your rankings.
Hope that makes sense.
If what you wish to include is static,
you do not have to use php includes.
Simply use includes for the static text:
<!--#include file="footer.html" -->
KISS & HTH
Sorry, I posted this question on several forums and I forgot about this one until I got the e-mail notification.
JohnNZ: Thanks very much for your reply. As I've heard, and as you've said: There will be a slight loading, but not too bad. However, even if it isn't too noticeable, my manager isn't too keen on the idea so I think I'll look into other ways.
Jonesy: Thanks for your reply. I attempted this, but it didn't seem to make any affect. However, I was only viewing it through notebook onto my browser. Would it only show an effect if I uploaded it onto my hosting server?
Okay, so I put my question to the test and it never worked on the hosting server. However, once I changed the filename to .shtml it worked. This bothers me as it means I'll have to change all of my pages to .shtml and this will just confuse Google.
I suppose nothing is ever simple.
All you need to get SSI to work on html files is something like
|AddHandler server-parsed .html |
or you could try
|AddOutputFilter INCLUDES html |
in your configuration
I tried out the SSI and it worked, however our page was very, very slow to load.
I use php includes in my pages for navigation and other things, like ads, headers and footers, etc.
I didn't notice any slowing of page loads when I implemented this, and, for all pages but the first downloaded, I imagine the loading should actually be faster, since the include is already in the browser cache.
You can search for 'css drop down menus' for lots of ways to do this simply.
All of my new pages are written as .php although they are in standard html.
I also wonder about a good way to change all of my old pages to .php, with minimum SE upset, but as yet have not investigated this too seriously.... since everything works with the .htaccess file's help!
|AddType application/x-httpd-php .htm .html |
This command instructs your web server to execute php code inside web pages with .html extensions
Does anyone have a clue on how this affects server load?
Thanks for any insights.
tbear: Thanks for your reply.
Huh, this is rather strange then. I wonder why it's loading slowly for us. In all honesty, the whole website needs revamped as when I first created it, we only had a few pages. Now that it's expanded, it's a bit of a mess.
I'm hesitant about using includes in case I do something wrong, but it will certainly make things easier.
Adding AddType application/x-httpd-php .htm .html to your htaccess file may or may not work, it depends on how your host's server is configured. The best way to get it right is to ask your host.
I went through the changes you describe several years ago for keeping navigation up to date. The .php includes will not show up on your desktop unless you are using a desktop server for your development.
Some things I learned from trial and error are:
1. make sure the character set and doctype for your includes matches the page they are included on. If you are using Windows the UTF-8 character set often uses "UTF-8 with BOM" that can leave a funny looking character in the page (it is a Byte-Order-Mark).
2. Clip out a block of text to use for your include that won't affect your page layout if for some reason it fails to process. Have more than one size (length) of page navigation to use as an include because you don't want people to view a page that is full of whitespace caused by a menu column that stretches the page length.
3. The best way I found to create your nav include is to use a page that you would like the other pages' navigation to resemble and simply cut out the text between the <container> tags whether div, td or p tags, paste it into a new text file and save it as nav.php or whatever you want your include to be called.
not2easy is quite right about asking your host to apply the .htaccss code. That is exactly what I did. Also regarding having a desktop server (I use xampp).
You can always create a test page, to put on your server, to try things out before using stuff site wide. That way you won't get any nasty surprises.
I have just implemented a multi-tiered drop down menu, just to keep the main menu size down to about 6 main categories, then using subs for each page. The two reasons are to contain the size and so as not to confuse visitors with too many choices.
Thank you very much not2easy and tbear!
If I have any more problems (which knowing me, I will) I shall be back. ;)