Not quite.... let's say this:
?page=some/path/page
The following code would get the path of the page to include, and include it:
<?php
$page = $HTTP_GET_VARS['page']; // Deprecated, use $_GET[] if you can....
// This checks to see whether we've accidentally added an extension
$ext = (strpos($page, '.', -6) !== FALSE)? (substr($page, strpos($page, '.', -6), 0)): 'php';
// $ext will hold the extension:
// 1.) Check to see if there is period at most 6 characters from the end
// (covers all common extensions (php, html, tpl, shtml...)
// 2.) If there is an extension, the extension is grabbed
// 3.) If no extension, default to php
// Now, remove the extension (makes the script more dynamic)
$page = (strpos($page, '.', -5) !== FALSE)?(substr($page, 0, strlen($page)-strpos($page, '.', -6))):$page;
// $page will hold the actual path and filename (w/o extension)
// 1.) Check to see if there is an extension
// 2.) If there is an extension, remove it and return the string that starts at
// position 0, and continues until the extension starts
// ** Could also be done like: $page = str_replace($ext, '', $page); **
// 3.) If no extension, just keep the same value.
if($page != '')
{
$pagefile = $page.'.'.$ext;
if(file_exists($pagefile))
{
include($pagefile);
}
else
{
include('404.php');
}
}
else
{
include('main.php');
}
?>
As long as this php file that includes the other files is in a parent directory, it will work.
What is wrong with your current code is that you are taking the path "path/page" and stripping the "/" off, as well as any "." so that this:
path/page.php
becomes
pathpagephp
What I've done is improved your code so this:
path/page.php
becomes
$page = path/page
$ext = php
~Brett