【问题标题】:Something wrong with PHPPHP 出了点问题
【发布时间】:2017-04-23 22:32:09
【问题描述】:

我正在尝试为我的网站创建一个类似论坛的部分。它没有发布,我不知道为什么。这是我的 PHP html

    <?php
if ($_POST) {
    $title = $_POST['title'];
    $name = $_POST['name'];
    $content = $_POST['commentContent'];
    $handle = fopen("comments.html", "a");
    fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3 
    class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
    fclose($handle);


    }

?>
    <form action="" method="POST">
    <textarea class="comment-boxmain" rows="20" cols="40" name="commentContent" 
    placeholder="Start Typing...."></textarea><br>
    <input class="comment-boxname" placeholder="Title" type="text" 
    name="title">
    <input class="comment-boxname" placeholder="Your Name" type="text" 
    name="name">
    <input class="comment-btn" type="submit" value="post"><br>
    </form>
    <?php include "comments.html"; ?>

如果有帮助,请查看 cvmblog.com/forum.php 上的答案。

【问题讨论】:

  • 嗯,会发生什么?旁注,此代码高度不安全。我可以将任何类型的讨厌的 HTML 放入您的 comments.html 中,例如重定向到恶意网站。
  • 您可以在表单的 action 属性中添加 .php
  • fwrite($handle, "

    $title


    按 $name

    $content

    ");
  • 我很抱歉.. 它什么也没做
  • 如果您使用此代码,您将有许多更严重的问题需要解决。

标签: php html


【解决方案1】:

字符串连接使用点 (.),而不是逗号 (,)。

替换:

fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3 
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");

与:

fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>". "<br>". "<h3 
class='Roboto-Slab'>By $name</h3>". "<p class='Roboto-Slab'>$content</p>");

它会起作用的。但是,这种串联是没有用的。你可以这样做:

fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3 class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");

还要检查 cmets.html 文件是否有 CHMOD 777。此外,在您的 php.ini 文件上启用 error_reporting,因为在这种情况下抛出的 PHP 错误可以很容易地引导您进入错误行。


以下是针对存储的 XSS(允许人们在您的页面上插入 HTML 和 Javascript 代码的漏洞)以及 RCE(远程代码执行)保护您的代码的实现:

 <?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
    $title = strip_tags($_POST['title']);
    $name = strip_tags($_POST['name']);
    $content = nl2br(htmlspecialchars($_POST['commentContent']));
    $handle = fopen("comments.html", "a");
    fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3 
    class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
    fclose($handle);


    }

?>
    <form action="" method="POST">
    <textarea class="comment-boxmain" rows="20" cols="40" name="commentContent" 
    placeholder="Start Typing...."></textarea><br>
    <input class="comment-boxname" placeholder="Title" type="text" 
    name="title">
    <input class="comment-boxname" placeholder="Your Name" type="text" 
    name="name">
    <input class="comment-btn" type="submit" value="post"><br>
    </form>
    <?php echo file_get_contents("comments.html"); ?>

另外,搜索一下数据库引擎(如果您还想使用文件,请查看平面文件数据库的实现,因为它被称为)。

【讨论】:

  • 谢谢大家。我让它工作起来了,但我能否获得一些关于如何让它更安全的说明?
  • 嗨,@WillHoffman!我建议你看看Damn Vulnerable Web App。它模拟了一个充满漏洞的站点以及有关它们的相应材料。在那里,您将了解 RCE(远程命令/代码执行)、XSS、SQL 注入等攻击的工作原理,以及防御它们和创建安全应用程序的方法,同时关注用户输入(主要是表单和上传)。 This也很酷。
  • 不知何故,我为与一个 10 岁的年轻开发人员分享黑客材料而感到内疚。不幸的是,它需要能够破解才能不被黑客入侵。我只希望你能很好地利用你在这里学到的知识。
猜你喜欢
  • 2016-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
相关资源
最近更新 更多