Dynamic Inclusion
Important The codes have changed for security reasons. Thanks to Jem from Tutorialtastic for the codes!
This will simplify your life even more than inclusions: no header.php nor footer.php! What are dynamic inclusions? I'm sure you've already seen URLs like index.php?action=random. Let's see the structure of a basic page again:
<!DOCTYPE>
<html>
<head>
<title>your page title</title>
</head>
<body>
// LAYOUT HEADER
// CONTENTS
// LAYOUT FOOTER
</body>
</html>
You only have to remove your contents and replace it by this:
<?php if (isset($_GET['x'])) {
if (strpos($_GET['x'], "/")) {
$dir = substr(str_replace('..', '', $_GET['x']), 0, strpos($_GET['x'], "/")) . "/";
$file = substr(strrchr($_GET['x'], "/"), 1);
if (file_exists($dir.$file.".php")) {
include($dir.$file.".php");
} else {
include("default.php");
}
} else {
if (file_exists(basename($_GET['x']).".php")) {
include(basename($_GET['x']).".php");
} else {
include("default.php");
}
}
} else {
include("default.php");
} ?>
Save the file as index.php and this is what it looks like:
<!DOCTYPE>
<html>
<head>
<title>your page title</title>
</head>
<body>
// LAYOUT HEADER
<?php if (isset($_GET['x'])) {
if (strpos($_GET['x'], "/")) {
$dir = substr(str_replace('..', '', $_GET['x']), 0, strpos($_GET['x'], "/")) . "/";
$file = substr(strrchr($_GET['x'], "/"), 1);
if (file_exists($dir.$file.".php")) {
include($dir.$file.".php");
} else {
include("default.php");
}
} else {
if (file_exists(basename($_GET['x']).".php")) {
include(basename($_GET['x']).".php");
} else {
include("default.php");
}
}
} else {
include("default.php");
} ?>
// LAYOUT FOOTER
</body>
</html>
What about the contents? Put them in a new file, contents only! No html, head or body codes! For example:
<p>Welcome dear visitor!</p>
Save it as default.php! If someone goes to index.php, the default.php contents will show up. What about the other contents?
<p>This is my about page.</p>
Only the text! No design codes. Save it as about.php! Now, for example, you want to access the about page? You just have to put a link to index.php?x=about ! No need to put about.php at the end of the url. It's already defined in the code. If you want to use something other than x as a variable, replace the bolded x by whatever you like. If your contents' files are .html, just change all the .php to .html :)
In this case, the URL will be something like: index.php?any=about. If your files are in a folder, your URL will look like this: index.php?any=folder/page.
PHP Dynamic Inclusion sample files (1.3 KB, 245 hits)
3 Responses to “Dynamic Inclusion”
Leave a Comment
Works? Doesn't work? Other tips? Please leave a comment! :)
i got it now.. but...
in the index page:
are you allowed to put some links?
in the default.php file and the other files..
are you allowed to put
textin the file?i mean.. i dont get it much..
aim me: dressedshock
Thank you! Your tutorial saved my butt.
I was previously using an older version of the PHP inclusion code that was (probably) insecure, and it didn't work when I moved to a webhost that runs a Windows server.
This new version that you have posted works perfectly! It even resolves the issue of a nasty looking PHP error if the page doesn't exist.
Good job.
I have the pages as you said to put them and I also have your sample files uploaded, but they don't work. Does the PHP Version have to be set to a certain version?