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 li
If you want the unparsed query string:
$this->input->server('QUERY_STRING');
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 get it like this:
$this->input->get('some_variable', TRUE);
See this for more info.
// 98% functional
parse_str($_SERVER['REQUEST_URI'], $_GET);
This in fact is the best way to handle the lack of support for $_GET query strings in CodeIgniter. I actually came up with this one on my own myself, but soon realized the same thing Bretticus did in that you had to slightly modify the way you treated the first variable:
// 100% functional
parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
It was only going to be a matter of time before I got to it myself, but using this method is a better one-line solution to everything else out there, including modifying the existing URI library, is isolated to only the controller where it is applicable, and eliminates having to make any changes to the default configuration (config.php)
$config['uri_protocol'] = "AUTO";
$config['enable_query_strings'] = FALSE;
With this, you now have the following at your disposal:
/controller/method?field=value
/controller/method/?field=value
Verify the results:
print_r($_GET); // Array ( [field] => value )
Here is how i did it recently. Hope it helps
<?php
//adapt this code for your own use
//added example.com to satisfy parse_url
$url="http://www.example.com".$_SERVER["REQUEST_URI"];
$url=parse_url($url);
//I'm expecting variables so if they aren't there send them to the homepage
if (!array_key_exists('query',$url))
{
redirect('/'); exit;
}
$query=$url['query'];
parse_str($query,$_GET); //add to $_GET global array
var_dump($_GET);
?>
to call : http://www.mydomain.com/mycontroller/myfunction/?somestuff=x&morestuff=y
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);
}
}
}
}
}
?>