这是使用 PHP 创建动态网页的好方法吗?

发布于 2024-12-10 06:58:06 字数 544 浏览 0 评论 0原文

我正在尝试使用 PHP 中的 include() 创建动态网页。 该 PHP 页面具有以下代码 - 从字面上看,这就是文件包含的全部内容:

<?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');
?>

您认为这是创建动态 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恏ㄋ傷疤忘ㄋ疼 2024-12-17 06:58:06

你所展示的完全没问题。使用数组可以更简单一些:

<?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');
?>

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');
?>
春风十里 2024-12-17 06:58:06

就我个人而言,我不喜欢 include() 函数来创建动态 php 页面。我发现很难避免变量在不同的 php 文件中发生冲突。假设您将在 NewsPage.php 文件中更改 $dbName,这可能会导致您的主页出现一些问题,并且您可能没有意识到出了什么问题。

我只包含带有类/函数的文件,这样我就可以控制包含的 php 文件中正在执行的代码,并且更容易调试我的代码。

Personally, I don't like the the include() function to create dynamic php page. I find it very difficult to avoid variables beeing conflicted in different php files. Let say that in NewsPage.php file you will change $dbName, it could result some problems in your main page and you might not realize what went wrong.

I only include files with classes/functions, that way i can control what code is beeing executed in the included php files, and it's more easy to debug my code.

悲念泪 2024-12-17 06:58:06

对于初学者来说这没问题,但请注意您的 DRY 问题,您添加的每个页面都将另外编写 3 行。
就这样维持下去是不可能的。
我看到您在 dbType 和 page 之间有一个名称约定,请尝试:

include($dbType + 'Page.php');

而不是 switch 块

It's fine for starters, but notice your DRY issue, each page you'll add, you will write another 3 lines.
It'll be impossible to maintain it this way.
I see you got a name convention between the dbType and page, try :

include($dbType + 'Page.php');

instead of switch block

对不⑦ 2024-12-17 06:58:06

这种风格的页面的名称是模板。它似乎与选择要显示的内容的任何其他方法一样有效。

The name for this style of page is a template. It seems as valid as any other way to choose which content to display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文