Original Post
I'm trying to make a page-loading mechanism. It's kind of hard to explain what I'm trying to achieve, but I'll explain it to the best of my ability. I have a website. On the website there are several files. However, they are not placed in the base directory like this:
home.php
about.php
contact.php
portfolio.php
Instead, they are placed in a directory called pages, and index.php in the base directory loads the files from the pages directory.
/index.php
/pages/home.php
/pages/about.php
/pages/contact.php
/pages/portfolio.php
Now to load a page, I would access my site like this:
http://mysite.com/index.php?page=about
And then the script in index.php would access /pages/about.php and then load the page. The contents of about.php would look something like this:
<?php
/**[About]**/
?>
<Content here>
When it's time to load the page, loadPage is called:
function loadPage($page)
{
if (file_exists(BASE . "/pages/" . $page . ".php"))
{
$file = fopen(BASE . "/pages/" . $page . ".php", "r");
fgets($file);
$title = str_replace(array("/**[", "]**/"), "", fgets($file));
fclose($file);
include BASE . "/section/html.php";
include BASE . "/section/header.php";
include BASE . "/section/leftmenu.php";
include BASE . "/section/rightmenu.php";
include BASE . "/pages/" . $page . ".php";
include BASE . "/section/footer.php";
}
else header("Location: /index.php?page=404");
}
And the code in index.php would look something like this:
<?php
loadPage( (!empty($_GET['page'])) ? $_GET['page'] : "home");
?>
So as you can see, it gets the page title, includes some menus located in the section directory, includes the page file in the page directory, and then includes the footer (another file in the section directory). The problem is though, I think there is a better way to do this. The problem is, I've been thinking about it for a while and I can't seem to find a better solution. I was considering using page templates and I have successfully implemented some, but then say if I add a menu, then I'd probably have to add a snippet of code (eg: {menu-3}) into each of the page files to have them include that menu. But is there a more flexible way of doing this? And can you comment on my method of approaching the problem? Thanks. Edit: I know the code above is vulnerable to LFI / RFI, but the real script filters slashes and null characters so there's no security hole