private function escape($string) {
if(get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}
使用PHP網站之一,遇到了上面的代碼,我是PHP的新手,我只是想了解上面的代碼是什麼。這個函數在PHP中做了什麼
請指導
private function escape($string) {
if(get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}
使用PHP網站之一,遇到了上面的代碼,我是PHP的新手,我只是想了解上面的代碼是什麼。這個函數在PHP中做了什麼
請指導
基本上這個函數對可能的SQL注入攻擊向量採取預防措施,如果它們存在並且確保只有沒有mysql代碼或其他奇怪字符的字符串被髮回到數據庫,則從這個字符串中剝離'"
這裏:if(get_magic_quotes_gpc()) $string = stripslashes($string);
: return mysql_real_escape_string($string);
但對於更多的細節,你真的應該看看這些功能的文檔,你所使用的PHP版本,因爲某些功能被棄用(未在最近版本的PHP中不再使用)
mysql_real_escape_string - 避免像」,」等 get_magic_quotes_gpc特殊字符 - 這是過時的PHP 5.3 version.it偵測魔法引號像/(斜槓)避免SQL注入是幫助後,
好吧,「的stripslashes」 - >取消引號引用的字符串。 我喜歡這個:
$ str =「你的名字是O \'reilly嗎?」; echo stripslashes($ str);
//輸出:是你的名字奧雷利?另一方面:「get_magic_quotes_gpc」 - >獲取「magic_quotes_gpc」的當前配置設置(PHP版本5.3.0中的DEPRECATED) - >設置GPC(Get/Post/Cookie)操作的magic_quotes狀態
所有的最後:mysql_real_escape_string - >爲在SQL語句中使用的字符串轉義特殊字符(它也是一個推薦的函數在PHP 5.5.0)
用於調試目的,你可以使用「迴響」。或代碼中的'var_dump'或'var_export'
指南:http://www.php.net/manual/en/function.stripslashes.php; http://www.php.net/manual/en/function.mysql-real-escape-string.php – hindmost
http://ch2.php.net/get_magic_quotes_gpc – mahatmanich