【问题标题】:Slashes appear in displayed form text despite using stripslashes尽管使用了斜杠,但斜杠仍出现在显示的表单文本中
【发布时间】:2014-01-10 07:41:19
【问题描述】:

我有一个评论框表单正在运行,我只是注意到当我使用' 时,它会在它前面创建一个\。例如,如果我写“how's”,它会在它发布到的数据库中以及它何时显示在页面上时都显示为“how\'s”。我在显示“正文”文本时使用了stripslashes 函数,我认为该文本应该去掉斜线。

我的服务器正在运行 php 版本 5.3.6,如果有帮助的话。

我正在使用这段代码将评论框正文中的文本发布到我的数据库中:

$body = mysql_prep($_POST['body']);

它使用的功能是这样的:

function mysql_prep($string) {
    global $connection;

    $escaped_string = mysqli_real_escape_string($connection, $string);
    return $escaped_string;
}

旁注:您不需要回答这个问题,但这是我用来使用户输入的文本免受黑客、坏人等所有攻击的功能。我做得好吗,还是我让自己对问题持开放态度? (除了这个可能是因为这个函数的斜线问题)

这是我用来显示文本的代码:

<div id="comments">
    <?php while($comment_text = mysqli_fetch_assoc($display_comments)) { ?>
    <div class="comment" style="margin-bottom: 2em;">
        <div class="author">
            <b><?php echo htmlentities($comment_text["author"]); ?>:</b>
        </div>
        <div class="meta-info" style="font-size: 0.8em;">
            <?php echo datetime_to_text($comment_text["created"]); ?>
        </div>
        <div class="body">
            <?php echo stripslashes($comment_text["body"], '<strong><em><p>'); ?>
        </div>
    </div>
    <?php } ?>  
    <?php if(empty($display_comments)) { echo "No Comments."; } ?>
</div>

非常感谢任何帮助或建议,谢谢!!

【问题讨论】:

  • 我没有看到stripslashes()。我只看到strip_tags()(它做了其他事情)。
  • 在准备 MySQLi 查询时,您将不需要 mysqli_real_escape_string() 函数。
  • 哦,天哪,你说得对@Gerald Schneider!我想我只是把它拿出来,然后放上stripslashes吧?除非我这样做是为了搞砸……
  • 实际上,我只是尝试将其切换出来,但我仍然会收到斜线......但我会更新我的问题以反映这一点
  • 它返回什么var_dump(get_magic_quotes_gpc())?是真是假?

标签: php mysql stripslashes


【解决方案1】:

你的问题是,你打开了Magic Quotes。您可以使用这种方法递归地去除所有斜线:

文件magic_quotes_filter.php

<?php

if (function_exists('set_magic_quotes_runtime') && get_magic_quotes_gpc()) {

    function stripslashes_deep($value) {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);  
}

然后你可以简单地include('path/to/magic_quotes_filter.php') 处理请求 HTTP 变量。

在您的情况下,只需将其包含在脚本的顶部即可。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2012-06-16
    • 2012-05-17
    • 2010-12-14
    相关资源
    最近更新 更多