Advanced Navigation using Includes

PHP Navigation tutorialA very simple way of doing the index.php?page=page_here. Includes a few configuration options.

Written in php4, works in php5, mostly so you can view the source and attempt at understanding how it works.

/*--- Configuration ---*/
 
$page = Array();
 
// Edit these vars or leave as is.
 
$page['home'] = "home";
 
// Replace this with the default or main page.
 
$page['error'] = "404";
 
// Replace this with the 404 page, can be 'home' also or even $page['home'].
 
$page['format'] = ".php";
 
// This will be the format of your page so ?page=bla will be bla.php.
 
$page['trig'] = "page";
 
// The trigger used so ?page=bla.
 
/*--- Do not edit below! ---*/if (empty($_GET[($page['trig'])])) {
 
$pg = $page['home'].$page['format'];
 
} elseif (!ctype_alpha($_GET[($page['trig'])])||!file_exists($_GET[($page['trig'])].$page['format'])) {
 
$pg = $page['error'].$page['format'];
 
} else {
 
$pg = $_GET[($page['trig'])].$page['format'];
 
}
 
include($pg);

ctype_alpha is used to make sure the “page” passed by the user only contains characters A-Z for security (so people can’t include /etc/passwd for example).

file_exists is used to determine whether the page + extension actually exist and if they don’t, the error page is then thrown.

An idea, if you want a readable page name from $pg, you could try

$page_name = ucwords(basename($pg,  $page['format']));

If you have any further questions, please do not hesitate to leave a comment,
I will update this snippet when features are suggested or questions and comments made.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)