【发布时间】:2015-01-08 18:33:37
【问题描述】:
我正在构建自己的 CMS 系统,我想从模板动态创建新页面。就像在 wordpress 中添加新页面一样。
这是模板:
<?php require_once('backend-nav.php');?>
<div id="main">
<div id="main-content" class="xlarge">
<article id="article-wrapper">
// My content needs to go here!
</article>
<?php require_once('backend-sidebar.php')?>
</div>
</div><!-- End main content container -->
<?php require_once('backend-footer.php')?>
<?php } else {
echo '<div class="container">You have to be logged in to view this page:.
'<ahref="login.php">Login</a>'.'</div>';
}
?>
我已经制作了一个表单来提交页面上我想要的内容,然后使用以下代码打开模板 php 文件并将其保存为服务器上的新文件,其中包含页面内容
$doc = new DOMDocument();
$doc->loadHTMLFile("new_page.php");
$article = $doc->getElementById('article-wrapper');
$p = $doc->createElement('p');
$addP = $article->appendChild($p);
$content = $doc->createTextNode($page_content);
$addP->appendChild($content);
// the url for the page is also submitted to the form and later added to the menu, which works.
$doc->saveHTMLFile($page_url.'.php');
因为没有 loadPHP 函数,所以我会使用这个函数。将链接添加到我的主菜单(也是一个 PHP 文件)时,它也对我有用。
现在内容被添加到文件中,并被相应地保存,但由于某种原因,它在某些地方搞砸了代码,像这样替换了一些符号?和 > 等等:
require_once('backend-header.php');
?>
<?php if (isset($_COOKIE['username'])) { ?>
<?php require_once('backend-nav.php');?>
<html>
<body>
<div id="main">
<div id="main-content" class="xlarge">
<article id="article-wrapper">
<p>test content added in p tags</p></article>
<?php require_once('backend-sidebar.php')?> </div><!-- End main content container -->
</div>
<?php require_once('backend-footer.php')?><?php } else {
echo '<div class="container">You have to be logged in to view this page: ' .
'<a href="login.php">Login</a>'.'';
}?></body></html>
替换html结束标签之前的PHP结束标签 我也尝试过 fread/write 来更改文件,但我可能没有以正确的方式使用。
有没有办法用 php 将代码添加到 php 文件中,或者有其他方法来获得我想要做的事情?
谢谢!
【问题讨论】:
-
虽然你可以这样做,但你不应该这样做。它更危险。如果您将 HTML 存储到数据库中并在 PHP 中打印出来,您就只有 javascript 注入的可能性。如果您将用户输入保存到 PHP 文件中,则您有可能会接管您的服务器的 PHP 注入。您应该改用 url 重写,并使用 db 来存储用户输入。
标签: php content-management-system domdocument read-write