Article written

  • on 15.09.2007
  • at 02:33 AM
  • by arkin

Advanced Navigation using Includes 0

Sep15

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.

subscribe to comments RSS

There are no comments for this post

Please, feel free to post your own comment

* these are required fields

arkinEx Codex is powered by WordPress and FREEmium Theme.
developed by Dariusz Siedlecki and brought to you by FreebiesDock.com