(PHP和HTML)在包含来自.txt文件的文本时如何不运行代码



html:

<html>
<head>
    <title></title>
</head>
<body>
    <header class="major last">
        <h2>Title</h2>
    </header>
    <form method="post" action="myprocessingscript.php" method="POST">
        <div class="row">
            <div class="6u 12u(mobilep)">
                <input type="text" name="Date" placeholder="Meeting Date (mm/dd/yy)" />
            </div>
            <div class="6u 12u(mobilep)">
                <input type="text" name="Topic" placeholder="Meeting Topic" />
            </div>
        </div>
        <div class="row">
            <div class="12u">
                <textarea name="Summary" placeholder="Summary of the Meeting" rows="6"></textarea>
            </div>
        </div>
        <div class="row">
            <div class="12u">
                <ul class="actions">
                    <li><input type="submit" value="Send Message" /></li>
                </ul>
            </div>
        </div>
    </form>
    <div>
        <p><?php echo file_get_contents( "data.txt" ); ?></p>
    </div>
</body>`

php:

<?php
if(isset($_POST['Date']) && isset($_POST['Topic']) && isset($_POST['Summary'])) {
    $data = 'Date: ' . ' ' . $_POST['Date'] . ' Topic: ' . $_POST['Topic'] . ' Summary: ' . $_POST['Summary'] . "n";
    $ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

我试图最终制作一个表格,以获取信息并将其记录在文本文件中。该文本文件最终将包含在网页中。但是,如果当前,如果代码(例如<a href="websiteaddress">websitename</a>)键入表单,则在包含该表单时,执行该代码。如何编辑代码将响应作为字符串而不是纯文本?

您可以在php中使用htmlspecialchars()函数。例如:

echo "<pre>";
echo htmlspecialchars('<a href="websiteaddress">websitename</a>');
echo "<pre>";

显然,一旦您编写了从文本文件中读取代码的代码后,您将提取一条或多个行并将变量放在htmlspecialchars()函数中。

一个谨慎的词:请注意某人将险恶的代码放入您的表格,例如JavaScript代码。查看XS和其他类似的攻击。由于您使用的是htmlspecialchars()这应该照顾大多数攻击,但要小心您如何实施代码。

最新更新