Welcome Guest, Not a member yet? Register   Sign In
Simple Query String Handler
#1

[eluser]virexmachina[/eluser]
After searching through the forums and wiki for a simple way to handle query strings in CI, I ended up writing a simple helper. I only needed to receive a query string on one page, so I didn't want to have to enable hooks, change uri_protocol or anything. This is good for cases where you need to get query strings on a page or two, like a search page or confirmations from payment gateways.

Anyway, here's a simple way to access the query string from any controller:

Code:
// for example, we're calling foo.com/search/?term=foo&bar=goo&baz;
$this->load->helper('query');
$qs = new Query_String();
$term = $qs->variable("term"); // returns foo
$bar = $qs->variable("bar"); // returns goo
$baz = $qs->variable("baz"); // returns true
$gaz = $qs->variable("gaz"); // returns NULL
$qsarray = $qs->all(); // returns Array([term] => foo, [bar] => goo, [baz] => NULL)

I tried to follow CI Input class conventions of returning null for variables that do not exist. I also return true if a var is provided with no value.

Download the helper to application/helpers and you're ready to go.

I adapted a loop from Dan Horrigan's CodeIgniter Query String hook to look for the query string in a few places.

Corrections and suggestions are very welcome.
#2

[eluser]Nick_MyShuitings[/eluser]
Congrats, I'll point a friend of mine to it, he was fighting with this and hacked something up to work with Twitter oAuth... should be interesting to see his comparison.
#3

[eluser]Tominator[/eluser]
$this->input->get() is not working for you?
#4

[eluser]virexmachina[/eluser]
[quote author="Tominator" date="1294263180"]$this->input->get() is not working for you?[/quote]

Nope, it doesn't work on Windows/Apache or CentOS/Apache for me.
#5

[eluser]Miroslav Milic[/eluser]
I had the same problem.
Thing is that Input library is destroying $_GET array when enable_query_strings is set to false and that is the case with standard CI configuration.

If you want to use $this->input->get() and $this->input->get_post() you can extend Input library with this code:
Code:
class MY_Input extends CI_Input {

    function MY_Input()
    {
        $CFG =& load_class('Config');

        // Remeber old setting
        $tmp = $CFG->item('enable_query_strings');

        // Set the enable_query_string to true so Input lib will not destroy $_GET array
        $CFG->set_item('enable_query_strings', TRUE);

        // In this special case we need to manualy parse the get query
        // (when using htaccess to hide index.php on some hosts)
        if ($CFG->item('index_page') == '' && $CFG->item('uri_protocol') == 'QUERY_STRING') {
            $get_array = array();
            parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $get_array);
            $_GET = $get_array;
        }

        // Initialize Input library
        parent::CI_Input();

        // Set the old setting back
        $CFG->set_item('enable_query_strings', $tmp);
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB