Does anyone know where I can learn to base my entire web site off of one index page? I can't seem to find anything.

Dani AI

Generated

A single index.php acting as a front controller is the right pattern here — it centralizes layout, auth, error handling and routing so each page is just a small content file. was on the right track with the single-entry idea and correctly pointed out small interpreter costs from unnecessary string interpolation. For a practical, safer pattern use an explicit route map (allowlist) instead of including whatever a user passes.

Example front controller (safe, easy to expand):

<?php
// public/index.php
declare(strict_types=1);

$routes = [
  ''       => __DIR__ . '/../pages/home.php',
  'home'   => __DIR__ . '/../pages/home.php',
  'about'  => __DIR__ . '/../pages/about.php',
  'contact'=> __DIR__ . '/../pages/contact.php',
];

$p = (string) filter_input(INPUT_GET, 'p', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
$page = $routes[$p] ?? null;

if (! $page || ! is_file($page)) {
    http_response_code(404);
    require __DIR__ . '/../pages/404.php';
    exit;
}

require $page;

Why this helps: it prevents directory traversal and arbitrary file inclusion by only ever loading files you explicitly list. For larger sites you can generate the $routes table from a database or config file (as hinted), but keep the same allowlist principle. Prefer require_once for shared bootstraps and include for optional fragments.

Troubleshooting and cautions: enable error reporting on development servers (error_reporting(E_ALL); ini_set('display_errors',1);) to catch path errors. Always use absolute paths (DIR) when including. Avoid directly including raw $_GET values; if you must, validate against a strict regex or use basename/realpath checks. Use proper 404 headers for missing pages and consider a rewrite rule (PATH_INFO) later for prettier URLs.

This gives you a single, maintainable entry point without the security pitfalls of naively requiring user-supplied filenames.

Recommended Answers

All 9 Replies

It's using function. You may take a look at

I know exactly what you want and I'm currently working on a tutorial to further explain in detail what one needs to understand/do to create a single file used to load other files, or a page to load other pages for a website.

It should be ready by tomorrow night. I'll be covering the query string, superglobals, dynamic file including, and basic security, all of which are involved and required when dealing with a dynamic page loader.

It's my first tutorial so I hope it works out well =]

That would be great. I REALLY need to see that. It would help a lot.

sorry imzac. the tutorial will have to wait. the final copy is 25% complete but I just ran into some problems (real life problems =\ ) so I will attempt to have it ready by tomorrow night. Keep checking back; I'll be providing plenty of tutorials (depending on how my first one goes) in the next few months. Sorry again!

Well, does anyone else know anything that would help me?

I'm not really familiar with PHP, but I imagine the code would look something like this:

<!-- All your header stuff -->

<?php
if ($content){
  require("$content");
} else {
  require('defaultcontent'); // replace defaultcontent with whatever file.
}
?>

<!-- All your footer stuff -->

The above could be used for the index page. The following would be used for the links.

<a "href=www.domain.com/index.php?content=aboutme.con">About Me.</a>

Anyone else have any feedback on this. I just came up with this on the fly from the little I do know about PHP. I didn't test this.

Alcides. :rolleyes:

Anyone else have any feedback on this. I just came up with this on the fly from the little I do know about PHP. I didn't test this.

Alcides. :rolleyes:

That's about right. I mean, obviously, it can get pretty complicated, as in my two software projects (sorry, only one's open source, and the open source one is incomplete at that).

http://www.daku.info/products/site_file_lite.html (just a beta)
http://www.minot.k12.nd.us/buildings

You may not notice it, but the second page is still justing using a base index.php file and employs page IDs beyond that point. However, it also uses error pages to redirect to index.php using shortcuts for each page. It's an elaborate, database-driven setup, but it allows us a great deal of flexibility, such as web-based administration.

require("$content");

This'll work, but it's a waste of the script interpreter's time:

require($content);

This is faster. (Without the double quotes this time - the double quotes tell the interpreter that there may be variables embedded in the string, and should evaulate those variables, so it goes searching for them. This takes marginally more time than knowing that they are there already. Obviously, it won't make much difference, but its good to know. )

Good point, Roberdin.

Alcides.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.