PHP Include function - don't fear it! It's not complicated and can be used on any page ending in .php and in conjunction with regular old html. And no, it's not a hacker's paradise...it's just a bit of code that'll call to a single file on your server and deliver the info in that file to your web page.
How is this useful? Well, if your sites end up growing tentacles like mine, you know it's a pain in the ass to maintain after a while. You add a new page or modify something and *bam* - you get to look forward to the tedium of modifying every blessed page that makes up your site. Loading up your, for instance, menu content in a text file and using the php include code to call to it, will make it so you only have to modify that *single* file for any menu changes to affect your whole site. Aye, there be a merciful god! Here's how: Step 1: Make sure your host supports php pages - most do now, and for free. There's nothing too special about the .php ending to your pages - indeed they can contain all html code. However, if you wish to use even a snippet of php code as this trick calls for, your page cannot end in .htm, .html, and so on. So yes, this would require renaming all your pages. But trust me - it's worth it! And the earlier in your site development you do this, the better. Once that's determined a go, rename all your site pages to end in .php Step 2: Open notepad or some other similar simple text editor. This document you'll fill with some sort of html code that you want to appear on your web page. Usually it's some info that repeats on several pages or throughout the entire site, like a menu for navigation. Put just that part of your page code in there (i.e. just the links to your menu). Keep in mind you can use the include function all over your page, so it's possible to make several of these files - one for menus, one for headers, one for footers, and so on. For this tutorial we'll keep it simple and pretend we're just doing a simple link menu. When you save the file, name it something like menu.inc - the .inc part is very important! Your text file has to save with that extension. Step 3: Upload this file into any and all directories on your website you want to be affected by the file. Step 4: Now, you gotta get the info in your .inc file to show up on your pages, right? This is where the php code comes in. Open on of your site's web pages. Wherever your menu was hard-coded in (or, if you're starting fresh and not modifying, wherever you'd want your menu to show up on the page), put this line of code: <?php include "menu.inc"; ?> Step 5: Upload your page with the php code intact, and watch your menu appear! If it's not positioned to your liking, modify by changing where you placed your php include code on the page. To change the menu links and so forth, modify your .inc file and upload it again. The advantage? Future menu tweaking will require one document be changed. If you have a few dozen to a few hundred pages that comprise your site, this thing is a life-saver! Troubleshooting: Problems? First, check and make sure every file has the right extension or ending. Remember, page name has to end in php and the file you're calling to has to end in .inc If you get an error where you put your php code snippet, odds are that's because the server can't find the file. So make sure .inc file is uploaded and uploaded to the right directory - generally the same folder your web pages are in. Once you get the hang of it, you can make as many .inc files as you want and use the same code above to call to each one - simply inserting that php code snippet where it should appear. It can all live happily alongside the rest of the html on your page and make upkeep and maitenance a real breeze! I know I can't live without it now! Main |
|
|