2014-01-10 38 views
0

我有一个注释框表单并且正在运行,而我刚注意到当我使用'时,它在它前面创建了一个\。例如,如果我写了“如何”,它会显示为数据库中发布的内容以及显示在页面上的“如何”。当我显示“body”文本时,我使用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> 

任何帮助或建议是超级感谢,谢谢!

+1

我没有看到'stripslashes()'。我只看到'strip_tags()'(它做了其他的事情)。 –

+0

在准备MySQLi查询时,您不需要'mysqli_real_escape_string()'函数。 –

+0

哦当然,你的右@Gerald Schneider!我想我会把它拿出来,然后放入反斜杠?除非我正在搞这样做...... – coolpup

回答

0

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

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