Welcome Guest, Not a member yet? Register   Sign In
question mark in the query string
#1

[eluser]junmin Liu[/eluser]
Hi, I have question about how to handle the query string contain question mark:

I have controller which has a method called

view($username, $title)

the url will be like
view/mike/what do you want?

The codeignitor will strip out the '?'. I did try urlencode('what do you want?'). It doesn't help.

the worse case is that ? in the middle of query value, like:
view/mike/what do you want?mike

the router will treat the above as 'view/mike', I don't know why?

please help.
#2

[eluser]Developer13[/eluser]
Hi junmin,

Would you rather allow the question mark in the URI string or would you rather use the url_title($string) function available from CodeIgniter's URL Helper?

Instead of "view/mike/what do you want?", url_title() would convert it to "view/mike/what-do-you-want" -- automatically removing spaces, question marks and other disallowed characters.
#3

[eluser]junmin Liu[/eluser]
the issue is that the user name 'mike' and title 'what do you want?' are the values stored in database.

if i search by mike/what-do-you-want, it will return null. and also user will definitely like to see their own title 'what do you want?' in the url.
#4

[eluser]Developer13[/eluser]
For purposes of maintaining standards, I'd recommend against the spaces and question marks in the URL, but that's just me.

If you do decide to use it, which it sounds like youu won't, you can have both a title column AND a url_title column in your database. The title column would contain the "what do you want?" and the url_title column will contain the "uri safe" version of that same string -- and the url_title is the column you would query against based on the safe characters in your URL.

Sorry for not directly answering your question - I'd prefer to answer it based on standards instead of allowing spaces and whatnot in the URL.
#5

[eluser]dcheslow[/eluser]
Why not just write your own encoder/decoder? I recently had a case where I needed to pass an email address on the URL... so I wrote these:

Code:
function encode($whence)
    {
        $whence = str_replace('@','0x100',$whence);
        $whence = str_replace('.','0x056',$whence);
        return $whence;
    }
    
    function decode($whence)
    {
        $whence = str_replace('0x100','@',$whence);
        $whence = str_replace('0x056','.',$whence);
        return $whence;
    }
[email protected] becomes my0x056name0x100here0x056there0x056com which is entirely legal in a URL
0x100 is just '0x' prepended to the hexidecimal value of ASCII @
0x is an odd enough sequence that real world collisions are rare
#6

[eluser]MikeHibbert[/eluser]
Hi

This code should do the trick:

Code:
$new_string = preg_replace('/[^a-zA-Z0-9 -]/','',$your_string );

It also removes '!' etc. I stole it from my slug function which is in a thread later in this section of the forums.

Mike
#7

[eluser]dcheslow[/eluser]
The OP says that he DOES NOT want to remove the special characters from the URL
#8

[eluser]missionsix[/eluser]
Quote:the router will treat the above as ‘view/mike’, I don’t know why?

Its routes to view/mike because that is how code igniter routes to controllers / methods. You can use routing to change this behavior.

If you are trying to use 'what do you want?' as a question to look up in a database, it is strongly suggested to normalize the url parameter as other people have suggested with a custom function or the default codeigniter function.

Then, store your normalized question in the database by adding a 'slug' field to the lookup table.




Theme © iAndrew 2016 - Forum software by © MyBB