Crawlicious

tools for web business

CodeIgniter Hook for handling GET

| 0 comments

This is in response to an earlier post about making GET work in Codeigniter

Basically I need GET in CodeIgniter, but I didn’t really want to apply the patch that I found on the Internet. I wanted a very simple solution, and I like the hook idea a lot better, so I followed the pattern from the second link, but it was by no means complete. Here is how I did it.

Enable hooks in system/application/config/config.php

$config['enable_hooks'] = TRUE;

Added a “pre_system” hook array into system/application/config/hooks.php

$hook['pre_system'] = array(
    'class'    => '',
    'function' => 'allow_query_string',
    'filename' => 'allow_query_string.php',
    'filepath' => 'hooks',
    'params'   => array()
);

Added a hook file system/application/hooks/allow_query_string.php
with the following contents

function allow_query_string() {
    if (strlen($_SERVER['QUERY_STRING']) > 0) {
        $temp = @array();
        parse_str($_SERVER['QUERY_STRING'], $temp);
        if (array_key_exists('token', $temp)) {
            $_POST['token'] = $temp['token'];
            $_SERVER['QUERY_STRING'] = "";
            $_SERVER['REDIRECT_QUERY_STRING'] = "";
            $_GET = @array();
            $loc = strpos($_SERVER['REQUEST_URI'], '?');
            if ($loc > -1) {
                $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 0, $loc);
            }
        }
    }
}

notes: your get parameters will appear in the $_POST array, it is very important that you keep that input safe by xss_clean’ing it

$this->input->xss_clean($_POST['token']);

In the allow_query_string.php you will notice that I am specific to ONLY allow the GET arg ‘token’ because that is all that I need right now. Later I may need to modify that, or eliminate it all together.

Leave a Reply