Raccoon Raccoon - 6 months ago 14
PHP Question

Is this a good way to create a dynamic webpage using PHP?

I'm trying to create a dynamic webpage using

include()
in PHP.
This PHP page has the following codes - literally, that's all the file contains:

<?php
session_start();
$dbName = $_REQUEST['DBName'];
$tbName = $_REQUEST['TBName'];
$dbType = $_REQUEST['DBType'];

include('header.php');
switch($dbType)
{
case 'Calender':
include('CalenderPage.php');
exit;
case 'News':
include('NewsPage.php');
exit;
case 'Gallery':
include('GalleryPage.php');
exit;
}
include('footer.php');
?>


Do you think it's a good way of creating dynamic PHP page?

Answer

What you show is perfectly fine. It could be done a bit simpler using an array:

<?php
session_start();
// Explicitly using $_GET or $_POST is better than $_REQUEST
$dbName = $_GET['DBName'];
$tbName = $_GET['TBName'];
$dbType = $_GET['DBType'];

include('header.php');

// be sure to have an array of allowed pages 
// so people can't access pages they're not supposed to access
$allowed_pages = array("Calender", "News", "Gallery");

if (in_array($dbType, $allowed_pages))
 include($dbType."Page.php");
else
 die("Unknown page");

include('footer.php');
?>