【问题标题】:Codeigniter Redirect -- The URI you submitted has disallowed charactersCodeigniter 重定向 - 您提交的 URI 包含不允许的字符
【发布时间】:2023-03-20 05:21:01
【问题描述】:

当我尝试重定向到其他网站时,我收到此错误:

遇到了 PHP 错误

严重性:警告

消息:parse_url(/%22**) [function.parse-url]: 无法解析 URL

文件名:core/URI.php

行号:219


遇到错误

您提交的 URI 包含不允许使用的字符。


这是我在 URI.php 中的所有代码

private function _detect_uri()
{
    if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
    {
        return '';
    }

    $uri = $_SERVER['REQUEST_URI'];
    if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
    {
        $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
    }
    elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
    {
        $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
    }

    // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
    // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
    if (strncmp($uri, '?/', 2) === 0)
    {
        $uri = substr($uri, 2);
    }
    $parts = preg_split('#\?#i', $uri, 2);
    $uri = $parts[0];
    if (isset($parts[1]))
    {
        $_SERVER['QUERY_STRING'] = $parts[1];
        parse_str($_SERVER['QUERY_STRING'], $_GET);
    }
    else
    {
        $_SERVER['QUERY_STRING'] = '';
        $_GET = array();
    }

    if ($uri == '/' || empty($uri))
    {
        return '/';
    }

    $uri = parse_url($uri, PHP_URL_PATH);

    // Do some final cleaning of the URI and return it
    return str_replace(array('//', '../'), '/', trim($uri, '/'));
}

【问题讨论】:

  • 正是它所说的 - 您是否尝试在 CI 源代码中搜索该错误消息并按照其路径找到检查这些字符的正则表达式?我敢打赌,更改正则表达式将解决问题...

标签: php codeigniter


【解决方案1】:

在我的例子中,这是一个格式错误的 URL。

就像mydomain/route&param=1

注意它的第一个参数应该有一个询问字符而不是一个“&”。 所以它应该是这样的: mydomain/route?param=1&other=2&another=3

【讨论】:

    【解决方案2】:

    CodeIgniter 检查所有URI 段中是否存在不允许的字符。这是通过将允许的字符列入白名单来实现的。可以在/system/application/config/config.php$config['permitted_uri_chars'] 变量中检查哪些是允许的。 permitted_uri_chars 是 CodeIgniter 在你的 URI 中接受的字符。默认值设置为类似。

    $config['permitted_uri_chars'] = 'a-z 0-9~%.:&_\-'; 
    

    默认情况下只允许这些:a-z 0-9~%.:_-

    留空以允许所有字符——但前提是你疯了。

    %22 用于"。您可以将其添加到permitted_uri_chars 列表中。

    【讨论】:

    • 是否可以在每个方法的基础上进行设置?意思是,我想允许@ 用于提交电子邮件地址的人,但只能在类的一种方法中。我希望它在其他任何地方都被禁止,而不必在每种情况下都检查它。
    【解决方案3】:

    试试这可能会有所帮助,但not recommended,在您的application/config/config.php 更改中:

    $config['permitted_uri_chars']  = ''; #keep it blank to allow all characters
    $config['allow_get_array']       = TRUE;
    $config['enable_query_strings'] = TRUE;
    

    【讨论】:

    • 这甚至不应该是一个选项,我认为这对 php 新手或不了解安全性的人来说是不好的教育。
    猜你喜欢
    • 2019-11-17
    • 2015-06-29
    • 1970-01-01
    • 2015-07-10
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多