简体   繁体   中英

CodeIgniter - Routing urls with GET Variables

I checked the similar questions out there, but those doesnt seem to help.

I have an email verification like, that is to be routed in CodeIgniter to the right function with data passed to the function for further processing.

Sample URL :

http://mysite.dev/verify/?id=emailaddress@gmail.com&hash=562828a975740ac6820e40f7f61b4407

Current Route :

$route['verify/(:any)'] = 'formcontroller/verification/$1';

Function :

public function verification($slug)
    {
        parse_str(parse_url($slug, PHP_URL_QUERY), $fileds);
        var_dump($fields);
    }

The problem is, I get a 404 when I try the url listed above. I get Message: Undefined variable: fields when I try something like http://amazon.dev/verify/asdasdasd

Can someone point me in the right direction?

ADD : If there is no ? in the url, it works. But for the standard of a get query url should be, I would like like to know how to solve this

Route doesn't receive query string as i understand. So, you should get variable from GET array. For this, 1st test that in config.php set

$config['allow_get_array'] = TRUE;

then in controller:

public function verification($slug)
    {
    $fields['id'] = $this->input->get('id');
    $fields['hash'] = $this->input->get('hash');
    var_dump($fields);
    }

The problem actually was, the query started after a / .

Actually the url should be in this model, ? immediately after the directory name.

http://mysite.dev/verify/?id=emailaddress@gmail.com&hash=562828a975740ac6820e40f7f61b4407

Route should be

$route['verify'] = 'formcontroller/verification';

And the variables should be handled using get method of CodeIgniter .

$this->input->get('variablename');

Also, the url should be urlencoded just incase if there are any characters which are not exclusively allowed by the config file.

Only the path segment of the URL is routable, and you don't need to parse the query parameters ...

Access them via the $_GET superglobal (like in raw PHP) or via the Input class' get() method .

the ? is not a permitted character in the URI segment in

/*
|--------------------------------------------------------------------------
| Allowed URL Characters
|--------------------------------------------------------------------------
|
| This lets you specify with a regular expression which characters are permitted
| within your URLs.  When someone tries to submit a URL with disallowed
| characters they will get a warning message.
|
| As a security measure you are STRONGLY encouraged to restrict URLs to
| as few characters as possible.  By default only these are allowed: a-z 0-9~%.:_-
|
| Leave blank to allow all characters -- but only if you are insane.
|
| DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!!
|
*/

which is located in application/config/config.php

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

you can append the ? to the list

here is a list of url encoded characters http://www.degraeve.com/reference/urlencoding.php

config.php文件

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM