2014-01-10 34 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變量。

就你而言,只需將它包含在腳本的頂部即可。