(5 ratings)   
By: arkinex.com
A very simple way of doing the index.php?page=page_here. Includes a few configuration options.
Added: 23 June 2008    Views: 122  
PathComputers    Programming    Php
Keywords: computers   programming   php   coder   code   language  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 
A 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.

About the Author :
arkinex.com
Advanced Navigation using Includes
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments