Kishor
Kishor

Reputation: 1513

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/[email protected]&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

Upvotes: 1

Views: 7046

Answers (5)

FelipeEduardo
FelipeEduardo

Reputation: 56

Config.php

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

Upvotes: 0

Kishor
Kishor

Reputation: 1513

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/[email protected]&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.

Upvotes: 1

Nassim
Nassim

Reputation: 2876

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

Upvotes: 0

splash58
splash58

Reputation: 26153

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);
    }

Upvotes: 2

Narf
Narf

Reputation: 14752

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.

Upvotes: 0

Related Questions