Tutorials
Dynamic includes
-
Dynamic includes
Posted on July 2, 2004
Written by
Cine (view more by Cine)
Comments (0)
Filed under PHP, Tutorials
You’ve probably seen pages that use cool URLs that look like ‘index.php?something’, but how do you create them?
Yes, there is a script on this site that tells you how to make something like this, but if you have a very big site, having all your content on one page can become rather confusing, and if you have a lot of content, it might even take a while to upload the file. So instead, we will create pages that look like index.php?something and have the script include the content of pages/something.php and put it between your header and footer tags. And it’s completely secure!
If you have read my tutorial on headers and footers you should already know how you can use the PHP function include() to include files into your page. That is almost the same thing as we’re going to do now.
First, create a new directory, and name it "pages". This is where we will put all our files that we want to be able to include. Since we want the front page to show up if there’s nothing specified after our ? in the URL, the first page we will create is named "main.php". My main.php looks something like this:
Welcome to my site! Blablablabla! Please sign my guestbook!<br /><br /> <b>Updates</b><br /> Today I blablabla!
But of course that’s not the only page I want on my site. I also want a page that can be accessed by going to index.php?pets. So therefore I will create a file in the pages directory, and name it pets.php. It looks like this:
I own two pets, one dog named Tjorven and a dwarf hamster named Newton. Here are some facts about them!<br /><br /> <b>Tjorven</b><br /> Age: 2<br /> Blabla: Bla!
Now create a new file in your HTML editor; this one is going to be in the directory above the /pages directory. Name the file index.php. This is where all our PHP code will be. Paste this into it:
<?php $page = basename($_SERVER['QUERY_STRING']);
include('header.php');
if(!$page){
include('pages/main.php');
} else {
if(file_exists('pages/'.$page.'.php')){
include('pages/'.$page.'.php');
} else {
echo('This page does not exist!');
}
}
include('footer.php');
?>
Now let’s go through the code!
<?php $page = basename($_SERVER['QUERY_STRING']);
The first thing we do is to open php. Then, we set the variable ‘page’ to contain what’s after the ? of our url. The basename function makes sure that the URL contains no directories or full paths, as all our content files are safely located in the pages directory, not in any higher or lower ones.
include('header.php');
Then we include the header of our layout, which here is named header.php. Change it if you want to.
if(!$page){
If there’s no page specified behind the ?…
include('pages/main.php');
Then include the main.php page.
} else {
But if there is something after the ?…
if(file_exists('pages/'.$page.'.php')){
Then we check whether the page we want to get exists, and that it is in the pages directory. The reason we want all our pages in that directory is so that nobody can include a file that you don’t want to be included. Security is important!
include('pages/'.$page.'.php');
If the file does exist, then we include it.
} else {
But if it doesn’t exist…
echo('This page does not exist!');
Then we tell them so! You can also include a 404 page here.
} }
It is important to close all your tags
include('footer.php');
Then, we include our footer file!
?>
And then, we close PHP.
That wasn’t so complicated, was it? Now, when somebody goes to index.php?stuff, pages/stuff.php will be included, so remember to only have the files you want to be accessed in that directory. Don’t put secretpage.php in it if you don’t want people to be able to see what it contains by just going to index.php?secretpage!
Since we have already included the header and footer in the index page, there is no need to include them in the stuff.php page. stuff.php only needs to contain the content, nothing more.
Comments
Comments are closed for this entry.