开发者

Where is this underscore coming from? I can only assume magic

I have the following test case controller setup in a codeigniter framework:

class Test extends Controller {
    public function Test()
    {
        parent::Controller();
    }

开发者_开发百科    public function index($bla = ''){
        echo $this->uri->segment(3) . '<br/>';
        echo $bla . '<br/>';

        $url = site_url('test') . '/index/' . rawurlencode('foo bar') . '/';

        echo "<a href=\"$url\">click me</a>";
    }

}

I would like $bla to echo 'foo bar', not 'foo_bar'. Regardless if I use rawurlencode, urlencode, or nothing at all, there is a '_' being inserted for the space character. I have confirmed the href for the anchor does include the %20 sign, so I don't understand why the string is being altered.

I have included two screen shots to illustrate the point, the first image shows when no argument is passed to the controller, the second is with the 'foo bar' passed to it. I just don't understand why 'foo%20bar' would be altered?

Where is this underscore coming from? I can only assume magic

Where is this underscore coming from? I can only assume magic


It works perfectly in Code Igniter 2.0 on PHP 5.3, but i'm guessing by your snippet that you're not using 2.0.

What I would suggest is try looking in the config file. I can't tell you what line number it is not using the same version as you but look for something like this

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

And making sure there's a space in it.

It may be that in older versions the segment function cleaned up the string before passing it back.

This is only a guess mind, but worth a shot.


I haven't tested this, but have you tried:

class Test extends Controller {
    public function Test()
    {
        parent::Controller();
    }

    public function index($bla = ''){
        echo $this->uri->segment(3) . '<br/>';
        echo rawurldecode($bla) . '<br/>';

        $url = site_url('test') . '/index/' . rawurlencode('foo bar') . '/';

        echo "<a href=\"$url\">click me</a>";
    }

}

I'll come back tomorrow when I have more time, but I imagine something funny is going on in the URI class. Or maybe I'm just missing it entirely.


I recommend running the following script outside of your codeigniter installation to see if it is your development environment that is encoding your url in an unexpected way.

<?php echo rawurlencode('foo bar');?>

if you get 'foo%20bar' then it must be a codeigniter problem. If it isn't, it is likely that your php environment is adding the underscore.


Try to change the config to

$config['uri_protocol'] = "QUERY_STRING";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜