I recently changed servers from Ubuntu to UNIX. On the Ubuntu install of MySQL and PHP, all strings needed to be escaped before inserted into the database, and were read from the database without extra escape characters. However, on the new system, strings that users enter into forms, when untouched, are inserted into the database fine when not escaped. If escaped, extra characters are added. But then I need to add an tag to the user-entered string with a simple .= and the string refused to be entered into the database unless escaped. And the original user-entered part of the string had extra escape characters. Is there any sort of easy solution for this? (PHP 5.2.17, MySQL 5.1, Apache 2.2.19)
1 Answer
A description about magic quotes and why it affects your script is here. Basically, this feature affects user input from the browser, but not other strings you create yourself.
Sounds like the relevant setting magic_quotes_gpc was off on your old server, but activated on the new one. I always remove magic quotes depending on the setting during runtime, to have the same setup on all machines, even if I can not control php.ini.
Try putting this in a central include file before any input parameter processing:
// Strip slashes from a string or array recursively
function stripshashesr($text)
{
if (is_array($text))
{
foreach($text as $key => $value) $text[$key]=stripshashesr($value);
return $text;
}
return stripslashes($text);
}
// Strip slashes from GPC
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
foreach($_GET as $key => $value) $_GET[$key]=stripshashesr($value);
foreach($_POST as $key => $value) $_POST[$key]=stripshashesr($value);
foreach($_REQUEST as $key => $value) $_REQUEST[$key]=stripshashesr($value);
foreach($_COOKIE as $key => $value) $_COOKIE[$key]=stripshashesr($value);
}
This removes magic quotes automatically depending on the setting.
magic_quotes_gpcis off?real_magic_escape_quotesor something like that, is for you.