Codeigniter PHP Framework - Need to Get Query String

How can I read a QueryString in CodeIgniter?

And yet, sometimes you need access to GET variables in CodeIgniter.

One glaring example is when you use an API that sends a post-back to your site (Paypal, etc.)

The easiest way, in my opinion, is to parse a server variable with the GET data you need since $_GET has been wiped (in my example, REQUEST_URI has my GET data.):

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

This allows the functionality exactly where you need it without requiring a global change to framework settings.

Here is a usage example.

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

Enabling $_GET in codeigniter

Add the following library to your application libraries. It overrides the behaviour of the default Input library of clearing the $_GET array. It allows for a mixture of URI segments and query string.

application/libraries/MY_Input.php

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

Its also necessary to modify some configuration settings. The uri_protocol setting needs to be changed to PATH_INFO and the '?' character needs to be added to the list of allowed characters in the URI.

application/config/config.php

$config['uri_protocol'] = "PATH_INFO";
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?';

It is then possible to access values passed in through the query string.

$this->input->get('x');

Enable Query Strings in Code Igniter

There are several ways to handle this.

Most People, and Elliot Haughin's Twitter Lib, extend the CI_Input library with a MY_Input library that sets allow_query_strings to true

You will also need to add ? to the allowed characters in config/config.php and set $config['url_protocal'] to PATH_INFO

see here: Enable GET in CodeIgniter

Custom URI routing by query string with CodeIgniter?

I highly recommend to use a SEF routing.

But if for any reason you're not eager to, you could check the query string inside the Accounts Controller, and then invoke the proper method, as follows:

Router:

$route['accounts/Auth.dll'] = "accounts";

Controller:

class Accounts extends CI_Controller
{
public function __construct()
{
# Call the CI_Controller constructor
parent::__construct();

# Fetch the query string
if ($method = $this->input->server('QUERY_STRING', TRUE)) {
# Check whether the method exists
if (method_exists($this, $method)) {
# Invoke the method
call_user_func(array($this, $method));
}
}
}

protected function signin()
{
# Your logic here
}
}

This allows you to invoke the methods by query string automatically.

what is the standard of query writing in codeigniter

If you have ever read about MVC architecture you would never ask this question. The name itself will tell you. M-model, where you write all your database related methods, C-controller where you write all your business logic and finally view which lets you show the desired view to the end user.

Coming to your question about Query, it is always recommended to write it in the model. There is no international standard or something. You can follow whichever suits your application. The CodeIgniter built-in query builder class is always recommended as CodeIgniter will handle all possible SQL Injections and attacks to the database. I personally suggest you use CodeIgniter query builder class. I've been using CodeIgniter more than 2 years from now.

If you have any doubts or reference, you can refer Codeigniter docs for the database. One of the best-documented framework I have ever seen.

$_GET contains URL string, and the actual query params are emptied -- Codeigniter

I ended up using this solution, found here: https://stackoverflow.com/a/2283881/1626354

I will say that this is more of a 'work-around' than a solution, but I can't invest anymore time in this right now.

Thanks everyone for your helpful suggestions. Hopefully this will be useful to someone else someday too.

querystring makes codeigniter fail

Use $this->input->get() (see Input Class), just making sure you have allow_get_array set to TRUE in your config.php file.

I believe this item was added in CI 2.0 and is on by default, so it now allows you to access GET data without needing to use enable_query_strings.

Not able to get data from url in codeigniter

check you have on in config/config.php

$config['allow_get_array'] = TRUE;

then try

$qry = $this->input->get('qry', TRUE);
$qry = $_GET['qry'];
$qry = $_REQUEST['qry'];

Also try url like

http://localhost.com/barbadosparliament/result?qry=testing



Related Topics



Leave a reply



Submit