url encoded forward slashes breaking my codeigniter app

隐身守侯 提交于 2019-11-27 22:16:19

I think the error message you are getting is not from codeigniter but from your web server.

I replicated this using Apache2 without even using CodeIgniter: I created a file index.php, and then accessed index.php/a/b/c - it worked fine. If I then tried to access index.php/a/b/c%2F I got a 404 from Apache.

I solved it by adding to my Apache configuration:

AllowEncodedSlashes On

See the documentation for more information

Once you've done this you might need to fiddle around with $config['permitted_uri_chars'] in codeigniter if it is still not working - you may find the slashes get filtered out

One way to get around this problem would be to replace any forward slashes in your URL variable which you are passing in the URI segments with something that would not break the CodeIgniter URI parser. For example:


$uri = 'example.com/index.html';
$pattern = '"/"';
$new_uri = preg_replace($pattern, '_', $uri);

This will replace all of your forward slashes with underscores. I'm sure it is similar to what you are doing to encode your forward slashes. Then when retrieving the value on the other page simply use something like the following:


$pattern = '/_/';
$old_uri = preg_replace($pattern, '/', $new_uri);

Which will replace all the underscores with forward slashes to get your old URI back. Of course, you'll want to make sure whatever character you use (underscore in this case) will not be present in any of the possible URIs you'll be passing (so you probably won't want to use underscore at all).

Raheel Khawaja

Change your permitted_uri_chars index in config file

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

With CodeIgniter, the path of the URL corresponds to a controller, a function in the controller, and the parameters to the function.

Your URL, /app/process/example.com/index.html, would correspond to the app.php controller, the process function inside, and two parameters example.com and index.html:

<?php
class App extends Controller {
    function process($a, $b) {
       // at this point $a is "example.com" and $b is "index.html"
    }
}
?>

edit: In rereading your question, it seems like you want to interpret part of the URL as another URL. To do this, you need to write your function to take a variable number of arguments. To do this, you can use the functions func_num_args and func_get_arg as follows:

<?php
class App extends Controller {
    function process() {
        $url = "";
        for ($i = 0; $i < func_num_args(); $i++) {
            $url .= func_get_arg($i) . "/";
        }

        // $url is now the url in the address
    }
}
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!