简体   繁体   English

PHP动态包括内容方法

[英]PHP dynamic include content approaches

I'd like to have the same header and footer on every page only with different contents using PHP. 我想在每个页面上使用PHP使用不同内容的页眉和页脚相同。 What is the best approach to do this? 这样做的最佳方法是什么?

I know of these two popular ones, but don't know which of them (or maybe some others) to choose: 我知道这两个流行的,但不知道他们中的哪一个(或可能是其他人)选择:

1. Include header and footer on every page 1.在每个页面上包含页眉和页脚

Into every page like contact.php , about.php , etc. you have to include header on top of the page and footer on the bottom. 进入每个页面,如contact.phpabout.php等,你必须在页面顶部包含标题,在页面底部包含页脚。 The content then goes between the too. 然后内容介于两者之间。 For example contact.php would look like this: 例如contact.php看起来像这样:

<?php include("header.php"); ?>
Lorem ipsum dolor sit amet...
<?php include("footer.php"); ?>

When you want to do request, you just type example.com/contact.php 如果您想要请求,只需输入example.com/contact.php

2. index.php?page=xyz 2. index.php?page = xyz

In pages like contact.php , about.php , etc. you store only the actual content. contact.phpabout.php等页面中,您只存储实际内容。 Then you have index.php with some parameter (eg page) by which you specify the content to be included. 然后你有index.php和一些参数(例如页面),通过它你可以指定要包含的内容。 For example the contact.php file would look like this: 例如, contact.php文件如下所示:

Lorem ipsum dolor sit amet...

And the index.php file like this (this is simplified version): 像这样的index.php文件(这是简化版):

<html>
    <head>
        <title>Lorem ipsum</title>
    </head>
    <body>
        <?php include(some_parser_function($_GET["page"].".php")); ?>
    </body>
</html>

Now when you do request for index.php?page=contact it will show you index.php and between the opening and closing body tags the content from contact.php . 现在,当您请求index.php?page=contact ,它将显示index.php以及开始和结束body标记来自contact.php的内容。

The first approach seems to me a bit unpractical, because you have to write the include functions into every page. 第一种方法在我看来有点不实用,因为你必须将include函数写入每一页。 In the second approach, if you want to have nice URLs like example.com/contact.php or just example.com/contact , you need to do apache mod_rewriting... 在第二种方法中,如果你想拥有像example.com/contact.php或只是example.com/contact这样的好网址,你需要做apache mod_rewriting ...

Which one of those (and others) approaches is better / more popular and why? 这些(和其他)方法中的哪一个更好/更受欢迎,为什么?

Thanks. 谢谢。

I would avoid dynamic includes if at all possible. 如果可能的话,我会避免动态包含。 It opens up a security hole unless you sanitize the variable that is coming from an external source, ie a GET parameter in your case. 它会打开一个安全漏洞,除非您清理来自外部源的变量,即您的情况下的GET参数。 You could take an approach like Model-View-Controller frameworks do. 您可以采用模型 - 视图 - 控制器框架之类的方法。 They have code that essentially puts the output from templates into the layout. 它们的代码基本上将模板的输出放入布局中。

Option 2 is much better for templating. 选项2更适合模板化。 You can also use Rewrite to make this appear without have ?page=pagename at the end of the url. 您还可以使用“重写”使其显示,而不会在网址末尾显示?page = pagename。 Also depending on how many pages you have on your site it is kind of a pain to have to do the two includes on every single file. 此外,根据您网站上的页面数量,每个文件必须包含两个页面,这是一种痛苦。

However if I were you I would look into using ob_start() ob_get_contents() and ob_flush(). 但是如果我是你,我会考虑使用ob_start()ob_get_contents()和ob_flush()。 Then you can actually run all the code on the included file before you start outputting html. 然后,在开始输出html之前,您实际上可以在包含的文件上运行所有代码。

Here is an example: 这是一个例子:

ob_start();
include("{$page_name}.php");
$page_contents = ob_get_contents();
ob_end_clean();

ob_start();
include("templates/standard.php");
$full_html = ob_get_contents();
ob_end_clean();

echo $full_html;

This way you can easily create multiple templates to be used at any point. 这样,您可以轻松创建多个模板,以便在任何时候使用。 A template file would look like this: 模板文件如下所示:

<html>
    <head>
        <title>Lorem ipsum</title>
    </head>
    <body>
        <?php echo $page_contents; ?>
    </body>
</html>

In general, the great advantage of the 2 approach is the verification/insertion. 通常,2方法的最大优点是验证/插入。 If you take the first approach, you will have to do all the verifications/insertions again and again. 如果采用第一种方法,则必须一次又一次地进行所有验证/插入。 And if you ever have to change/add/remove something, you will have to change all the pages. 如果您必须更改/添加/删除某些内容,则必须更改所有页面。 That is quite annoying. 这很烦人。 I usually prefer the 2nd approach, since you can centralize everything and avoid repeating the same instructions all around. 我通常更喜欢第二种方法,因为你可以集中所有内容并避免重复相同的指令。

But that depends a lot on your website too. 但这在很大程度上取决于您的网站。

I'll give you an example: I once did a site that had 2 main divisions. 我举个例子:我曾经做过一个有两个主要部门的网站。

In this case I did 2 different pages of the 2nd approach. 在这种情况下,我做了第2种方法的2个不同页面。

(when I'm talking about verifications, I mean things like verifying all the SESSION variables, if the user is logged, if he is ADM etc etc) (当我谈论验证时,我的意思是验证所有SESSION变量,如果用户被记录,如果他是ADM等等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM