Navigation

In this tutorial we will look in detail into how to use PHP to include content into the main homepage. This can be used to as an alternative to an iFrame and can be used to help save disk space and allow easier updating of common areas by only having to edit one page. I will also touch on a simple

So let’s start first we must create an array which will hold all the page locations and page names. The format will be “page_reference” => “page_location”, Which in turn would make the url show as index.php?page=page_reference

$pages = array("homepage" => "main.php", "about" => "about.php", "contact" => "contact_us.php", "404" => "error.php");

So following that format you are able to add as many pages as you want. There is also another advantage of using this method as just referencing the pages directly as if there were no checks to see whether the page is allowed to be public this way then this can be exploited to include other files not authorized so this is vital to help maintain security and lower risks.

Next we will set some variables;

$default_choice

This is used when ?page= in the url is not set. This is the page reference as it will still check the page against the array we created earlier.

$current_choice

Using the $_GET array we will retrieve from the url the ?page= variable and using strip_tags command we will remove any coding tags which could be used to exploit the script and we will also remove any blank spaces at the start or end of the variable using the trim command.

$error_page

This is used in the event of a request of a page that does not exist in the array previously created or the actual page not existing.

Now we have what they mean lets actually put the code in.

$default_choice = "homepage";
$current_choice = trim(strip_tags($_GET['page']));
$error_page = "404";

This is all the variables and settings done with. Next we need to check whether there is a page selected and if there is a page selected whether the page exists. We do this by using the isset function. In simple terms the isset function is used to determine if the string has been set. So we will use this to determine if $current_choice has been set in the url and if the current choice has been set if a page with that reference can be found within the array we created at the start.

if(isset($current_choice, $pages[$current_choice]))
{

Now we have checked if the page exists in the array, we still aren’t sure if the actual file exists on the server. This is done using the file_exists function.

if(file_exists($pages[$current_choice]))
{

Next is to include the actual file that is being requested.

include($pages[$current_choice]);

Once we have confirmed the page exists we need to actually bring it in to the current page we have done this using the include function and using the $pages array we created at the beginning we select the page that the user has selected.

But now we have included the page if the file exists but what happens when the file is unable to be located. Well this is where the 404 reference page comes in.  If the file is not found then we include the error page.

} else {
	include($pages[$error_page]);
}

Finally we finish this tutorial by completing the first if statement. If no page has been selected then we must include a default page which in our case is “homepage”. Although for an extra point you will notice that there is a @ sign before they include function on this part this is because we aren’t running any checks whether the default page actually exists. The @ symbol is used as a silencer. A silencer is used to hide any errors should they occur. This includes any errors such as not being able to locate the file or if there are any errors such as PHP errors on the page that has been included.

You are welcome to add checks for file existing simply copy the format as listed in a previous part of this tutorial.

} else {
	@include($pages[$default_choice]);
}

But now for what it should look like J.

<?php

$pages=array("homepage"=>"main.php","about"=>"about.php","contact"=>"contact_us.php","404"=>"error.php");

$default_choice="homepage";
$current_choice=trim(strip_tags($_GET['page']));
$error_page="404";

if(isset($current_choice,$pages[$current_choice]))
{
	if(file_exists($pages[$current_choice]))
	{
		include($pages[$current_choice]);
	}
	else
	{
		include($pages[$error_page]);
	}
}
else
{
	@include($pages[$default_choice]);
}

?>

A demo of this tutorial can be found at

http://www.peter-kelly.com/downloads/navigation_test_files/

Should you have any further questions about this tutorial or any other tutorials listed on my website or have any other suggestions for tutorials. Please don’t hesitate to contact me. I hope this tutorial helps you and if you have any feedback about how I can improve the tutorials I produce I welcome it.

Leave a Reply