CodeIgniter不允许使用关键字符

Joh*_*and 32 codeigniter

CodeIgniter给了我一个Disallowed Key Characters错误.我已经将它缩小到表单字段的name属性:name='prod[50-4121.5]'但我不知道该怎么做.

Wal*_*ter 34

问题是您使用的是未包含在标准正则表达式中的字符.用这个:

!preg_match("/^[a-z0-9\x{4e00}-\x{9fa5}\:\;\.\,\?\!\@\#\$%\^\*\"\~\'+=\\\ &_\/\.\[\]-\}\{]+$/iu", $str)

根据评论(和个人经验),你不应该修改他们的Input.php文件 - 相反,你应该创建/使用自己的文件MY_Input.php,如下所示:

<?php

class MY_Input extends CI_Input {

    /**
     * Clean Keys
     *
     * This is a helper function. To prevent malicious users
     * from trying to exploit keys we make sure that keys are
     * only named with alpha-numeric text and a few other items.
     * 
     * Extended to allow: 
     *      - '.' (dot), 
     *      - '[' (open bracket),
     *      - ']' (close bracket)
     * 
     * @access  private
     * @param   string
     * @return  string
     */
    function _clean_input_keys($str) {
        // UPDATE: Now includes comprehensive Regex that can process escaped JSON
        if (!preg_match("/^[a-z0-9\:\;\.\,\?\!\@\#\$%\^\*\"\~\'+=\\\ &_\/\.\[\]-\}\{]+$/iu", $str)) {
            /**
             * Check for Development enviroment - Non-descriptive 
             * error so show me the string that caused the problem 
             */
            if (getenv('ENVIRONMENT') && getenv('ENVIRONMENT') == 'DEVELOPMENT') {
                var_dump($str);
            }
            exit('Disallowed Key Characters.');
        }

        // Clean UTF-8 if supported
        if (UTF8_ENABLED === TRUE) {
            $str = $this->uni->clean_string($str);
        }

        return $str;
    }

}

// /?/> /* Should never close php file - if you have a space after code, it can mess your life up */
Run Code Online (Sandbox Code Playgroud)

++汉字支持

// NOTE: \x{4e00}-\x{9fa5} = allow chinese characters
// NOTE: 'i' — case insensitive
// NOTE: 'u' — UTF-8 mode
if (!preg_match("/^[a-z0-9\x{4e00}-\x{9fa5}\:\;\.\,\?\!\@\#\$%\^\*\"\~\'+=\\\ &_\/\.\[\]-\}\{]+$/iu", $str) { ... }

// NOTE: When Chinese characters are provided in a URL, they are not 'really' there; the browser/OS
//   handles the copy/paste -> unicode conversion, eg:
//        ???  -->  xn--4gqsa60b   
//   'punycode' converts these codes according to RFC 3492 and RFC 5891.
//   https://github.com/bestiejs/punycode.js ---  $ bower install punycode
Run Code Online (Sandbox Code Playgroud)


Rob*_*itt 27

打开libraries/Input.php(system/core/Input.php在CI版本2.0+中)并找到function _clean_input_keys($str){,整个块应如下所示:

function _clean_input_keys($str)
{
    if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
    {
        exit('Disallowed Key Characters.');
    }

    return $str;
}
Run Code Online (Sandbox Code Playgroud)

修改它允许新字符的PCRE sot.

请注意,缺少的字符是.(点),你应该总是.在正则表达式中转义(点),否则它们将允许任何单个字符.

/^[a-z0-9:_\/-\.]+$/i
Run Code Online (Sandbox Code Playgroud)

  • 为什么接受的答案是CodeIgniter hack? (28认同)
  • 请记住创建MY_Input类以扩展CI_Input类.不要在那里硬编码.[阅读本文](http://ellislab.com/codeigniter/user-guide/general/core_classes.html). (7认同)
  • @rubdottocom:正如我在CodeIgniter 2.1.3中看到的那样,`_clean_input_keys`方法在`CI_Input`类中未被声明为私有,因此您可以在`MY_Input`中扩展此方法而不会出现任何问题. (3认同)

Kha*_*ari 9

要将CodeIgniter与jQuery Ajax一起使用,请使用"Object"作为数据而不是Query字符串,如下所示:

$.ajax({
    url: site_url + "ajax/signup",
    data: ({'email': email, 'password': password}), //<--- Use Object
    type: "post",
    success: function(response, textStatus, jqXHR){
        $('#sign-up').html(response);
    },
    error: function(jqXHR, textStatus, errorThrown){
        console.log("The following error occured: "+
                    textStatus, errorThrown);
    }
});
Run Code Online (Sandbox Code Playgroud)