紅茶小館

一杯紅茶喜相逢 Share Web Tech and Life …

PHP magic_quotes

做 backend DB 常常要處理 single quote, double quote 在 sql 裡的問題,
尤其是php 不知server 是否 開啟了 get_magic_quotes_gpc() 設定,
所以用addslashes並不是好方法.
在網上找到這個處理方法, 很不錯,或者用 str_replace把
single quote, double quote 變成 htmlentities。

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET);
  $_POST = array_map('mysql_real_escape_string', $_POST);
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}else
{
   $_GET = array_map('stripslashes', $_GET);
   $_POST = array_map('stripslashes', $_POST);
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET);
   $_POST = array_map('mysql_real_escape_string', $_POST);
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.