PDA

View Full Version : dynamic inclusion


Steph
Sun 23rd May '04, 4:17am
Hi all,

I thought while our graphic designer works on the template for my forum, Id work on other sections of my site. I'm no pro at programming. I know some basic php and qbasic and not including html/css, that's pretty much it.

Anyways, I was trying to use the code provided on this site to accomplish dynamic inclusion: http://www.regretless.com/dodo/newworld/ (d inclusion 2) where you have something like: http://mysite.com/test.php?mypage and whenever I try this code it goes to the default variable listed as main.php, instead of the file i'm trying to call (in my above scenarion - mypage.php). If there is a better way to do this than presented in this tutorial or if someone can enlighten me on what I'm doing wrong. It would be greatly appreciated.

the test.php page looks like this:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> test </title>
</head>
<body>
<?php
if($z) {
// make sure it's a file in this particular directory
// $_server['query_string'] gives you everything after
// the question mark
$z = basename($_server['query_string']);
// make php extension for it
$z .= ".php";
include($z);
} else {
include("main.php");
}
?>
</body>
</html>

Steph
Sun 23rd May '04, 5:39am
Hi guys,

Nevermind I figured out a way to do it using conditional statements, $_SERVER['QUERY_STRING'] == ""), and includes.

:)

sirspot
Mon 24th May '04, 10:24am
Using register globals can cause you problems down the line. Consider adding the following just before you begin using your $z variable (assuming z is a variable passed in through the URL)

$z = $_GET['z'];



and since you are using include, you may want to do this instead to avoid problems when the requested file does not exist.


$success = @include($z);
if($success == false)
{
$locationOf404ErrorPage = '404.html';
header("Location: $locationOf404ErrorPage");
exit();
}