简体   繁体   English

php中的魔术报价Gpc的解决方法

[英]workaround for magic quotes Gpc in php

The servers have magic quotes on and it cannot be turned off for some reason.....now stripslashes would remove all the slashes added by magic quotes,but if user has put slashes in the input field(we are allowing slashes), the stripslashes would remove that as well. 服务器上已启用了魔引号,并且由于某些原因无法将其关闭.....现在,反斜杠会删除由魔引号添加的所有斜杠,但是如果用户在输入字段中输入了斜杠(我们允许斜杠),则条状斜杠也会将其删除。

I am trying for a regex which would remove the slashes only if it is preceded by {',",}.... 我正在尝试使用仅在{',“,} ....之后才删除斜杠的正则表达式。

Any help would be appreciated. 任何帮助,将不胜感激。

I use this code in config file: 我在配置文件中使用以下代码:

// remove slashes, if they are being automatically added
if ( get_magic_quotes_gpc () ) {
    $_GET    = array_map('stripslashes', $_GET);
    $_POST   = array_map('stripslashes', $_POST);
    $_COOKIE = array_map('stripslashes', $_COOKIE);
    $_REQUEST = array_map('stripslashes', $_REQUEST); // see ThiefMaster's comment
}

That's the only place where I have to worry about slashes. 那是我唯一担心斜线的地方。 In all other places I can safely assume that there are no "magic quotes". 在所有其他地方,我可以放心地假设没有“魔术引号”。

Though, stripslashes() does not work with arrays, so if you expect to have arrays as values in $_GET, $_POST or $_COOKIE (in public web pages/systems, you DO expect to have arrays), this function might be used as a callback: 虽然, stripslashes()不适用于数组,所以如果您希望将数组作为$ _GET,$ _ POST或$ _COOKIE中的值(在公共网页/系统中,您确实希望具有数组),则可以使用此函数作为回调:

function stripslashes_recursive($value) {
    if ( is_array($value) ) {
        return array_map(__FUNCTION__, $value);
    }
    return stripslashes($value);
}

PHP 5.3 users might use a closure as a callback: PHP 5.3用户可以使用闭包作为回调:

$stripslashes = function($value) use(&$stripslashes) {
    if ( is_array($value) ) {
        return array_map($stripslashes, $value);
    }
    return stripslashes($value);
};

This won't pollute a global scope with additional function ( stripslashes_recursive() ). 这不会污染具有附加功能的全局范围( stripslashes_recursive() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM