简体   繁体   English

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

[英]Dynamic PHP Page Include (CMS / Blog)

I have hunted around, but have not found an answer specific to what I am trying to do. 我已经四处寻找,但没有找到针对我要执行的操作的答案。 I currently run Wordpress on my personal web server, but I am looking to move away from it. 我目前在个人Web服务器上运行Wordpress,但是我希望不再使用它。 I am writing my own semi-CMS system. 我正在编写自己的半CMS系统。 I have a of it already done, and am starting to do the Blog section. 我已经完成了其中一个工作,现在开始做Blog部分。 I have a bare-bones pages template that I use when creating a blog entry saved in a /blog_files directory. 创建一个保存在/ blog_files目录中的博客条目时,我使用了一个准系统页面模板。 These pages just include a <section> area with formated text. 这些页面仅包含带有格式文本的<section>区域。 From there, I have a PHP script that grabs the 5 most recent PHP files and include them into the Index.php page. 从那里,我有一个PHP脚本,可捕获5个最新的PHP文件并将其include在Index.php页面中。 This works perfectly and seems super fast. 这可以完美运行,而且看起来超级快。

My question/problem is that i am looking for a secure way to have visitors be able to click on the blog heading and take them to a dedicated page for that blog. 我的问题/问题是,我正在寻找一种安全的方法,以使访问者能够单击博客标题并将其带到该博客的专用页面。 I have an empty page.php page that includes header, menu, and footer, and would like to include the blog_page/file.php inside of that page. 我有一个空的page.php页面,其中包含页眉,菜单和页脚,并希望在该页面中include blog_page / file.php。 I want to do this dynamically, but also securely. 我想动态地但安全地执行此操作。 I have thought about using sessions or GET/POST, but not sure which would be the best for performance ans security. 我曾考虑过使用会话或GET / POST,但不确定哪种对性能和安全性是最好的。

I came across this page which includes the following: 我碰到了该页面 ,其中包括以下内容:

    <?php
    session_start();
    $_SESSION['regName'] = $regValue;
    ?>

    <form method="get" action="get_reg.php">
        <input type="text" name="regName" value="">
        <input type="submit">
    </form>

I thought of using this, and just passing the include page though this variable, but this is code is from 6 years ago, and I am not sure if this is still the preferred way to handle this. 我想使用它,只是通过此变量传递include页面,但这是6年前的代码,而且我不确定这是否仍然是处理此问题的首选方法。 I am running Centos 7 with Apache 2.4 and latest PHP. 我正在使用Apache 2.4和最新的PHP运行Centos 7。

Any help would be appreciated. 任何帮助,将不胜感激。

Well, after quite a bit of digging, reading, combining, editing, re-editing, and re-re-editing (lol) I think I have found my solution. 好吧,经过大量的挖掘,阅读,合并,编辑,重新编辑和重新编辑(大声笑)之后,我想我找到了解决方案。

This is what I ended up with. 这就是我最后得到的。

This is the blog page which shows the 5 most recent blog files in the blog_files directory. 这是博客页面,显示blog_files目录中的5个最新博客文件。 It then creates creates a link with the header information which passes the filename to the blog_single page. 然后,它使用标题信息创建一个链接,该链接将文件名传递到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;

        }

    ?>

From there, the blog_single page gets the file name and then include that into the page. 从那里,blog_single页面获取文件名,然后includeinclude到页面中。

    <?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>

I hope this helps someone in the future, and if you notice anything I should have done, please feel free to post your suggestions or comments. 希望这对以后的人有所帮助,如果您发现我应该做的任何事情,请随时发表您的建议或意见。

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

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