If you've ever used this simple include script, you'll know it can make maintaining a website much easier. If you use the same layout on every page, with the include function you can keep the layout in header and footer files for easy updating, and just include those two files in every page.
You can do it the other way around, too. If you've ever been to a site that uses URLs like www.domain.com/index.php?page=about, that's what they're doing. index.php is the layout, and "about" is the included content file. Sound complicated? Keep reading.
What the url index.php?page=about does is set the "$page" variable to the value "about." With PHP code we then say "if variable $page is equal to "about," include "about.inc" in the file." Below is the actual code:
if ($page=="about") {
include ("about.inc");
} The reason we do this, rather than simply saying include ("$page.inc"), is just in case someone tries to put their own URL filled with nasty code into your pages, which they'd do like this: www.domain.com/index.php?$page=http://www.hackers.com/hackfile. To prevent this we put a list of acceptable include files, and a warning message if anyone tries something like that. The complete code would look like this: <?php
if ($page=="hello") {
include ("hello.inc");
}
elseif ($page=="about") {
include ("about.inc");
}
elseif ($page=="links") {
include ("links.inc");
}
elseif ($page=="") {
include ("main.inc");
}
else {
echo "That is not a valid location!";
}
?> Notice how the line that says elseif ($page=="") calls "main.inc"? That's for when someone just goes to www.domain.com/index.php with no ?$page= on the end.To use it, just copy and paste it between the header and footer (the <body> and </body> tags) of your index.php file.if ($_SERVER['QUERY_STRING'] == "about") {
include("about.inc");
} With the above code, you'd use URLs like this: index.php?about.<?php
switch($_SERVER['QUERY_STRING']) {
case 'hello':
include('hello.inc');
break;
case 'about':
include('about.inc');
break;
case 'links':
include('links.inc');
break;
default:
include('main.inc');
}
?> Much neater, and it works faster, too. 




© 2000-2010 Xentrik.Net