By using PHP server-side includes, you can greatly increase the scalability and ease of maintenance of your Website.
A PHP include, or server-side include, is a simple HTML or PHP file that sits outside of your main HTML/Web document. It usually contains code that is re-used on multiple pages of your website, such as site news or random quotes. When needed, you call the include via a line of PHP code.
Includes should be used in any situation where you will be re-using code. By using an include, you can make changes across multiple pages in your site by editing one file—a nice timesaver. Common uses of include files are footer navigation, copyright statements, navigation menus, site news, random quotes, and date scripts, to name a few.
Though you could use includes whenever you want, it’s not good practice to do so unless absolutely necessary. If the content is not reused on multiple pages or you don’t have a slick dynamic script that requires them, you don’t need to use includes. Include files put a small amount of extra strain on the server, resulting in a longer (usually unnoticeable) load time. If you overuse includes for every small bit of code, you may notice a larger difference in your site’s load time, deterring and frustrating your visitors.
Let’s assume you have a large website. At the top of each page you have your company name and slogan. What would happen if you needed to update your slogan on every page? This isn’t brain surgery, but it could take quite a bit of time. Additionally, since you will be separately updating multiple files, there’s a good possibility you might misspell a word on one or more of the pages. PHP includes is a perfect solution to a challenge like this one.
Here’s an example of a basic PHP file (we’ll call it index.php):
<html>
<head>
<title>My PHP Home Page</title>
</head>
<body>
PHP tutorials are fun. Woohoo!
</body>
</html>
Here’s our entire include file to include the company name and slogan (we’ll call it inc_header.php):
<h1>Acme Baseball Gloves: Catch The Game</h1>
To include company name and slogan contained in inc_header.php, we only need to add one line of code:
<?php include ("inc_header.php"); ?>
index.php now looks like:
<html>
<head>
<title>My PHP Home Page</title>
</head>
<body>
<?php include ("inc_header.php"); ?>
PHP tutorials are fun. Woohoo!
</body>
</html>
That’s it! For every other page in the site, you only need to add one line of code to insert the company name and slogan. When the slogan changes, simply open up inc_header.php, change the text, upload it, and the update is made across every page in your site that uses that file.
PHP is slightly different than other Web programming languages in that it allows multiple methods of including files. The example in this tutorial is adequate 99% of the time, but more advanced users might also be interested in the other methods. Note: for each of these examples, the included file is “inc_header.php”. This will change depending on the name of your include file.
Make it a point to give your includes a .PHP extension only. The reason is this—if someone snooping around in your site guesses the correct filename for your include file and the extension ends in .inc (or some other extension), the full source code will be displayed in the browser. This can be a huge security flaw, especially when your include file contains database connection strings, passwords, etc. If the snooper guess correctly but your include ends in .PHP, the file will be processed first (the server recognizes it as a server-side programming file), and the person will only see the OUTPUT of the code, not the source code itself.





May 16th, 2007 at 2:10 am
This is a very nice an well designed webpage.
Regards,
George