CodeIgniter PHP Framework - Need to get query string

Viewed 109456

I'm creating an e-commerce site using CodeIgniter.

How should I get the query string?

I am using a Saferpay payment gateway. The gateway response will be like this:

http://www.test.com/registration/success/?DATA=<IDP+MSGTYPE%3D"PayConfirm"+KEYID%3D"1-0"+ID%3D"KI2WSWAn5UG3vAQv80AdAbpplvnb"+TOKEN%3D"(unused)"+VTVERIFY%3D"(obsolete)"+IP%3D" 123.25.37.43"+IPCOUNTRY%3D"IN"+AMOUNT%3D"832200"+CURRENCY%3D"CHF"+PROVIDERID%3D"90"+PROVIDERNAME%3D"Saferpay+Test+Card"+ACCOUNTID%3D"99867-94913159"+ECI%3D"2"+CCCOUNTRY%3D"XX"%2F>&SIGNATURE=bc8e253e2a8c9ee0271fc45daca05eecc43139be6e7d486f0d6f68a356865457a3afad86102a4d49cf2f6a33a8fc6513812e9bff23371432feace0580f55046c

To handle the response I need to get the query string data.


Sorry, I haven't explained the problem clearly. I am getting a 'Page not found' error while getting the response from the payment site after payment.

I have tried enabling with uri_protocol = 'PATH_INFO' and enable_query_strings = 'TRUE' in config.php. While googling I found this won't work if I use htaccess rewrite.

I have already tried changing the config entries, but it doesn't work.

12 Answers

I have been using CodeIgniter for over a year now. For the most part I really like it (I contribute to the forum and use it in every instance that I can) but I HATE the ARROGANCE of that statement in the manual:

Destroys the global GET array. Since CodeIgniter does not utilize GET strings, there is no reason to allow it.

The presumption that you will never need GET in a CodeIgniter application is asinine! Already in just a few days, I've had to deal with post back pages from PayPal and ClickBank (I'm sure there are a million others.) Guess what, they use GET!!!

There are ways to stop this GET squashing, but they are things that tend to screw other things up. What you don't want to hear is that you have to recode all your views because you enabled querystrings and now your links are broken! Read the manual carefully on that option!

One that I like (but didn't work because setting REQUEST_URI in config.php broke my site) is extending the Input class:

class MY_Input extends CI_Input
{
        function _sanitize_globals()
        {
            $this->allow_get_array = TRUE;
            parent::_sanitize_globals();
        }
}

But the best no-nonsense way is to test with print_r($_SERVER) at the URL where you need the GET variables. See which URI Protocol option shows your GET variables and use it.

In my case, I can see what I need in REQUEST_URI

// defeat stupid CI GET squashing!
parse_str($_SERVER['REQUEST_URI'], $_GET);

This places your query string back into the $_GET super global for that page instance (You don't have to use $_GET, it can be any variable.)

EDIT

Since posting this I found that when using REQUEST_URI, you will lose your first query string array key unless you remove everything before the ?. For example, a URL like /controller/method?one=1&two=2 will populate the $_GET array in this example with array('method?one'=>1,'two'=>2). To get around this, I used the following code:

parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);

I suppose I should have provided an example, so here goes:

class Pgate extends Controller {
   function postback() {
      parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
      $receipt = $this->input->xss_clean($_GET['receipt']);
   }
}

If you want the unparsed query string:

$this->input->server('QUERY_STRING');

Open up application/config/config.php and set the following values:

$config['uri_protocol'] = "PATH_INFO";

$config['enable_query_strings'] = TRUE; 

Now query strings should work fine.

Thanks to all other posters. This is what hit the spot for me:

    $qs = $_SERVER['QUERY_STRING'];
    $ru = $_SERVER['REQUEST_URI'];
    $pp = substr($ru, strlen($qs)+1);
    parse_str($pp, $_GET);

    echo "<pre>";
    print_r($_GET);
    echo "</pre>";

Meaning, I could now do:

$token = $_GET['token'];

In the .htaccess i had to change:

RewriteRule ^(.*)$ /index.php/$1 [L]

to:

RewriteRule ^(.*)$ /index.php?/$1 [L]

You could make a rule in your .htaccess to prevent your MOD_REWRITE from firing on that specific page. That should allow you to use the _GET.

You can create a pre_system hook. In the hook class you create, you can grab the desired query params and add them to the $_POST for normal CI processing. I did this for a jQuery Ajax helper.

For instance:

(Name this file autocomplete.php or whatever you put as the file name in the hook)

<?php

/*
By Brodie Hodges, Oct. 22, 2009.
*/

if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*   Make sure this file is placed in your application/hooks/ folder.
*
*   jQuery autocomplete plugin uses query string.  Autocomplete class slightly modified from excellent blog post here:
*   http://czetsuya-tech.blogspot.com/2009/08/allowing-url-query-string-in.html 
*   Ajax autocomplete requires a pre_system hook to function correctly.  Add to your 
*   application/config/hooks.php if not already there:

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

*                               
* 
*/

class Autocomplete {
    function override_get() {
        if (strlen($_SERVER['QUERY_STRING']) > 0) {
            $temp = @array();
            parse_str($_SERVER['QUERY_STRING'], $temp);
            if (array_key_exists('q', $temp) && array_key_exists('limit', $temp) && array_key_exists('timestamp', $temp)) {
                $_POST['q'] = $temp['q'];
                $_POST['limit'] = $temp['limit'];
                $_POST['timestamp'] = $temp['timestamp'];
                $_SERVER['QUERY_STRING'] = "";
                $_SERVER['REDIRECT_QUERY_STRING'] = "";
                $_GET = @array();
                $url = strpos($_SERVER['REQUEST_URI'], '?');
                if ($url > -1) {
                    $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 0, $url);
                }
            }
        }
    }
}

?>
Related