2011-09-08 26 views
15

這個問題已經在不同的地方提出了幾次,但我還沒有找到明確而明確的答案。大多數解決方案都涉及到人們說要禁用php.ini文件中的Magic Quotes(我所做的)或修改核心WP文件。

無論如何,問題是這樣的:爲什麼每次使用$ wpdb-> insert或$ wpdb->更新斜線都會在任何單引號之前添加。因此,例如:

我吃過草莓成爲我\'維生素E吃草莓

下面是我使用的示例代碼:

$id = $_POST['id']; 
$title = $_POST['title']; 
$message = $_POST['message']; 

$wpdb->update('table_name', array('id'=>$id, 'title'=>$title, 'message'=>$message), array('id'=>$id)) 

同樣的問題這裏:Wordpress Database Output - Remove SQL Injection Escapes但它從來沒有解決從「禁用魔術引號」

+0

你確定你有魔術引號禁用?你在'phpinfo()函數檢查的話' – zerkms

+0

是啊,這裏是從的phpinfo摘錄 - > magic_quotes_gpc的\t關\t關 magic_quotes_runtime的\t關\t關 magic_quotes_sybase \t關\t關 –

+0

在那些額外/缺失報價的'更新()'調用拼寫錯誤或實際上在你的代碼? – Phil

回答

39

經過這一天花了一天之後,答案如下:

Wordpress轉義$ _POST聲明,而不是在實際插入,這是奇怪的。

$id = stripslashes_deep($_POST['id']); //added stripslashes_deep which removes WP escaping. 
$title = stripslashes_deep($_POST['title']); 
$message = stripslashes_deep($_POST['message']); 

$wpdb->update('table_name', array('id'=>$id, 'title'=>$title, 'message'=>$message), array('id'=>$id)); 

這樣做意味着WP不會在任何引號前添加斜線。

+0

非常方便!謝謝! – geilt

+3

拯救生命。謝謝@J李。如果不在這裏,這肯定需要一些時間進行調試。 – Nick

+0

它工作正常....感謝buddy – kuldipem

5

多一點信息 - 即使您從3.0版開始關閉它,WordPress決定讓人們認爲他們會瘋狂地添加「魔術引號」。任何對$ _REQUEST,$ _GET,$ _POST,$ _COOKIE或$ _SERVER的訪問都將受到影響。請參閱wp-includes/load.php

/* Add magic quotes to $_GET, $_POST, $_COOKIE, and $_SERVER. 
* @since 3.0.0 
*/ 
function wp_magic_quotes() { 
     // If already slashed, strip. 
     if (get_magic_quotes_gpc()) { 
       $_GET = stripslashes_deep($_GET ); 
       $_POST = stripslashes_deep($_POST ); 
       $_COOKIE = stripslashes_deep($_COOKIE); 
     } 

     // Escape with wpdb. 
     $_GET = add_magic_quotes($_GET ); 
     $_POST = add_magic_quotes($_POST ); 
     $_COOKIE = add_magic_quotes($_COOKIE); 
     $_SERVER = add_magic_quotes($_SERVER); 

     // Force REQUEST to be GET + POST. 
     $_REQUEST = array_merge($_GET, $_POST); 
} 
2

的WordPress忽略內置的PHP魔術引號設置和get_magic_quotes_gpc()的值 ,並隨時添加魔術引號(甚至 後的特徵是從PHP 5.4中刪除)。

你可以用這個代替

//replace $_POST with $POST 
$POST  = array_map('stripslashes_deep', $_POST); 
$wpdb->insert( 
     'wp_mytable', 
     array( 
      'field_name'  => $POST['field_name'], 
      'type'    => $POST['type'], 
      'values'   => serialize($POST['values']), 
      'unanswered_link' => $POST['unanswered_link'], 
     ), 
     array( 
      '%s','%s','%s','%s' 
     ) 
    ); 

的WordPress做是因爲太多的內核和插件代碼已經到了 依靠報價在那裏,在超級 全局因此禁用引號(正如在上面的「基本示例」和「Good Coding Practice」中所做的那樣)可能會導致安全漏洞。

http://codex.wordpress.org/Function_Reference/stripslashes_deep