php - 尽管使用了条斜杠,斜杠仍出现在显示的表单文本中

标签 php mysql stripslashes

我有一个评论框正在运行,我刚刚注意到当我使用 ' 时,它会在它前面创建一个 \ 。例如,如果我写“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>

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

最佳答案

你的问题是,你有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 变量。

就您而言,只需将其包含在脚本的顶部即可。

关于php - 尽管使用了条斜杠,斜杠仍出现在显示的表单文本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21038636/

相关文章:

php - 将值从数组插入数据库

php - stripslashes 安全测试

php - 从 MySQL 结果中转义 PHP 中的引号 (PDO)

PHP 如何找出 memcached 数组的文件大小

php - 列出一个目录下的所有文件 PHP

mysql - 将 SELECT 语句与搜索的 CASE 表达式结合使用

PHP - 使用字符串的 stripshales 后出现斜线

javascript - 使用 jQuery 运行 php 函数

php - Openshift,503 服务不可用。没有服务器可用于处理此请求

php - MYSQL WHERE 子句是错误的值