动态PHP页面包含(CMS /博客)


Dynamic PHP Page Include (CMS / Blog)

我已经四处寻找,但没有找到特定于我想要做的事情的答案。我目前在我的个人网络服务器上运行Wordpress,但我希望摆脱它。我正在编写自己的半CMS系统。我已经完成了其中的一部分,并开始做博客部分。我有一个基本的页面模板,我在创建保存在/blog_files 目录中的博客条目时使用。这些页面仅包含一个带有格式化文本的<section>区域。从那里,我有一个PHP脚本,它获取5个最新的PHP文件并将它们include到Index.php页面中。这非常有效,看起来超级快。

的问题/问题是,我正在寻找一种安全的方式来让访问者能够单击博客标题并将他们带到该博客的专用页面。我有一个空白页面.php页面,其中包含页眉、菜单和页脚,并希望在该页面内include blog_page/文件.php。我想动态地做到这一点,但也要安全地做到这一点。我考虑过使用会话或 GET/POST,但不确定哪个最适合性能和安全性。

我遇到了这个页面,其中包括以下内容:

    <?php
    session_start();
    $_SESSION['regName'] = $regValue;
    ?>
    <form method="get" action="get_reg.php">
        <input type="text" name="regName" value="">
        <input type="submit">
    </form>

我想过使用它,只是通过这个变量传递include页面,但这是 6 年前的代码,我不确定这是否仍然是处理这个问题的首选方法。我运行的是带有Apache 2.4和最新PHP的Centos 7。

任何帮助将不胜感激。

好吧,经过相当多的挖掘、阅读、组合、编辑、重新编辑和重新编辑(笑),我想我已经找到了我的解决方案。

这就是我最终得到的。

这是博客页面,显示blog_files目录中的 5 个最新博客文件。然后,它会创建一个包含标题信息的链接,该链接将文件名传递到blog_single页面。

    <?php
        $blogs = array(); // create blog file array
        // gathers all files in blog_file folder matching *.php
        foreach (glob("blog_files/*.php", GLOB_BRACE) as $filename) {
            $blogs[$filename] = filemtime($filename);           }
        arsort($blogs);
        // return only the newest 5 files
        $newest = array_slice($blogs, 0, 5);
        // for each of the newest 5, gather meta tag info from page
        foreach($newest AS $blog => $value) {
        $tags = get_meta_tags($blog);           
        $title=$tags['title'];
        $authur=$tags['author'];
            // if page title is not empty proceed
            if (!empty($title)) {
            // strip folder and .PHP from file name or security and to use as title
            $page = basename($blog, ".php").PHP_EOL;                    
            // echo $title and link to blog_single while passing variable
            echo("<h3><a href=blog_single.php?page=$page>$title</a></h3>");
                }
        // include blog entry in page below title link
        include $blog;
        }
    ?>

从那里,blog_single页面获取文件名,然后将其include到页面中。

    <?php 
        // reconstruct include file path
        $page = "blog_files/" . $_GET["page"] . ".php";
        // I will be adding additional code to verify PHP file exists only in includes directory
        // get meta-tag information from page
        $tags = get_meta_tags($page);           
            $title=$tags['title'];
            if (!empty($title)) {
                $headline=$title ;
                }   
    ?>
    <section>
        // include blog entry in the blog_single.php page
        <?php include($page) ;?>
    </section>

我希望这对将来的某人有所帮助,如果您注意到我应该做的任何事情,请随时发布您的建议或意见。