url encoded forward slashes breaking my codeigniter app

后端 未结 4 1565
借酒劲吻你
借酒劲吻你 2020-12-06 01:34

i’m trying to create a url string that works like this:

/app/process/example.com/index.html

so in other words,

/app/process         


        
相关标签:
4条回答
  • 2020-12-06 02:01

    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).

    0 讨论(0)
  • 2020-12-06 02:01

    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
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-06 02:16

    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

    0 讨论(0)
  • 2020-12-06 02:17

    Change your permitted_uri_chars index in config file

    $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
    
    0 讨论(0)
提交回复
热议问题