|
 FreewareVista Great Freeware Freeware and free trials, tips and resources
Using a PHP include
As can often be the case, one can search amidst a haystack of information much of which is quite advanced, unable to find key basic information. The information below may prove helpful for you if you want to use the PHP include function.
Just a bit of background - the PHP include function is a means of duplicating content on various web pages without the need to update each web page individually. This can also be done using frames or inline frames. The difference is that with PHP, your server places one page inside the other, and presents the combined result to the browser. With frames, the browser does the work.
How to do it:
One simply places the include command into their web page at the desired location.
The structure to use is: <?php include("page to include.html"); ?>
For example, let's say you have a menu of items you want to include on each page of your website. You have named this menu: menu.html.
You would then create the command <?php include("menu.html"); ?> and place it where (in the main page, let's call it main.html) you want the menu to appear.
This will cause menu.html to be included inside of main.html
Note that this will include all of menu.html, inside of main.html, so you'll want to remove certain tags such as the html, head, body, and meta tags from menu.html
Now here's a key bit of information that you may have great difficulty finding by searching the web, it's really one of those 'too simple to be mentioned' sort of points. But if you don't know PHP at all, nothing is too simple, and hours later you can be pulling out your hair because no one thought to mention this one key 'obvious' point.
Here it is, on a shared hosting plan, for the PHP include function to work, you have to name the main page with the PHP file type. (thus main.html must be changed to main.php) Your html editor should allow you to simply save main.html as main.php, and you can edit main.php just as you would any html file.
As a note, you can also change the file to be included to a php file, thus menu.html becomes menu.php, just make sure you change the include command to: <?php include("menu.php"); ?>
By the way, if you have any difficulty, call your hosting company to make sure they have your server properly configured, they will likely also look at your .php file and confirm you have the correct syntax.
I hope the above sorts out some confusions and saves you some time.
|