Allowing url query string in codeigniter

As we all know codeigniter uses segment-based as url, example:

Just like these:


Unfortunately, I was developing a web application that would accept payments via paypal. And I've use the NVP (Name Value Pair) API. This API returns to your page by calling a query base url which has the authentication token. Of course the codeigniter will pop out a url error message.

1.) I've found out that the codeigniter system can be overridden using the hook class.

2.) So first enable hooks in application/config.php
$config['enable_hooks'] = TRUE;

3.) In the hooks documentation codeigniter/general/hooks.html, there are several hook points, we will use the pre_system, because we need to get the $_GET variables before it is read.

4.) In application/config/hooks.php, add the following line:

$hook['pre_system'] = array(
'class' => 'Paypal', //classname
'function' => 'override_get',
'filename' => 'override_get.php',
'filepath' => 'hooks',
'params' => array()

5.) Create a file inside, application/hooks named: override_get.php and create the following function:

function override_get() {
if (strlen($_SERVER['QUERY_STRING']) > 0) {
$temp = @array();
parse_str($_SERVER['QUERY_STRING'], $temp);
if (array_key_exists('your_get_variable', $temp)) {
$_POST['your_post_variable'] = $temp['your_get_variable'];
$_GET = @array();
$url = strpos($_SERVER['REQUEST_URI'], '?');
if ($url > -1) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 0, $url);
Allowing url query string in codeigniter Allowing url query string in codeigniter Reviewed by czetsuya on Sunday, August 02, 2009 Rating: 5

No comments:

Powered by Blogger.