pass parameters in URL code igniter

968 Views Asked by At

I am new to CI and would normally try to pass some information back to a login page like the following...

public function authenticate()
{
    $this->load->model('User_model', '', true);

    if ($this->User_model->authenticate($this->input->post('username'), $this->input->post('password')))
    {
        $this->session->set_userdata('loggedin', true);
        header('Location: /');
    }
    else
    {
        header('Location: /sessions/login/error?='.'1');
    }
}

And then on the login page use a _GET.

So how would I best go about this in CodeIgniter?

2

There are 2 best solutions below

0
On

From my understanding you want to pass _GET variables for the error codes, right? as i see you are using _POST variables for the form as you should.

As you are working with Codeigniter, you can use the URI structure of the framework and instead of working with _GET work with the MVC structure.

So, instead of redirecting this:

header('Location: /sessions/login/error?='.'1');

Redirect like this:

header('Location: /sessions/login/error/1');

Then in your controller for the login, in the error method:

function error($msgId = 0) {
  // here you will get the number you sent in the url as $msgId
}

If you still need to work with _GET variables, that is possible as well. go to the main application config file of the framework /aplication/config/config.php and have a look on the Enable Query Strings section

0
On

First of all, using GET for sensitive data like email/password is a general no-no. Bad practice all the way.

But the answer to your question is here - http://www.codeigniter.com/userguide3/libraries/input.html

$this->input->get('username') is equal to $_GET['username']. You might use $this->input->get('username', TRUE) to escape some malicious code.

And $this->input->post('username') is equal to $_POST['username']. This is what I advise you to use (it requires modification of your HTML though)