[英]PHP Escaping query string variables
我在我的Web應用程序中創建了一個表單,該表單只有一個文本字段,並且該字段使用GET發布到PHP頁面,但我觀察到奇怪的行為。 即,當我在本地服務器上測試時,文本是在文本字段中寫入時收到的,但是當我將其上傳到我的在線服務器時,收到的字符串會自動轉義,所有單引號和雙引號都會被轉義。 例如,如果我寫It's not true...
然后在PHP方面我會得到
$comment = $_REQUEST["comm"];
print $comment;
//will print It\'s not true... on my online server
//will print It's not true... on my local server
我無法理解為什么會如此? 是否有自動轉義查詢字符串變量的PHP設置?
你啟用了“魔術引號”。 它們是一個可怕的錯誤,幸運的是在下一個版本的PHP中被刪除。 PHP手冊有一個禁用它們的指南 。
簡而言之,您需要在php.ini
文件中將以下配置項設置為Off
:
具體來說,你的問題似乎是magic_quotes_gpc
- “gpc”部分是“ GET ,POST和COOKIE”的縮寫 - 但最好是禁用所有這些部分。
代碼會告訴你每件事你需要什么..
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string"); // i.e. PHP >= v4.3.0
if ($new_enough_php) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if ($magic_quotes_active) {
$value = stripslashes($value);
}
$value = mysql_real_escape_string($value);
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if (!$magic_quotes_active) {
$value = addslashes($value);
}
// if magic quotes are active, then the slashes already exist
}
return $value;
}
為這個函數創建上面的函數和傳遞值
然后調用像這樣的值
$yourVar = mysql_prep($_POST['yourControlName']);
我希望你可以通過評論解釋所有事情......
我認為它是php.ini文件中的一個設置。 您可以調用PHP函數來禁用它,但到那時為時已晚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.