msc msc
msc msc

Reputation: 75

Codeigniter re-direct after user logs in?

When a user logs into their account I want that user re-directed back to the page they were currently visiting verses the members area section.

Below is the page I want my visitor to see after they login. After they login I want them re-directed to the below page.

http://www.localhost.com/folder/this-is-the-restricted-page

This is what I’m doing. One the restricted page I’m adding the below syntax (to the URL above)

$current_url = current_url();

$this->session->set_userdata('redirect',$current_url); 

Then after my function validates the user I added this syntax (below)

if($this->session->userdata('redirect')){

            redirect($this->session->userdata('redirect'));

        } else {

 $this->session->set_userdata($data);

            redirect('members/members_area',$data);
 } 

This above syntax isn’t working. I’m not sure what I’m doing wrong. Would anyone know what/where I’m going wrong?

Thanks

PS. Below is my entire function that validates when a user logs in.

function validate_credentials(){
   $this->load->model('Members_data');
   $query = $this->Members_data->validate_users();
    if($query){
    $id = $this->Members_data->get_member_ID($this->input->post('username'));
    $email = $this->Members_data->get_member_email($this->input->post('username'));
    $data = array(
       'username' => $this->input->post('username'),
    'id' => $id,
    'email' => $email,
    'is_logged_in' => TRUE
     );

        if($this->session->userdata('redirect')){

              redirect($this->session->userdata('redirect'));

              } else {

    $this->session->set_userdata($data);

          redirect('members/members_area',$data);
     }


    } else {
       $this->login();
    }
 } 

Upvotes: 0

Views: 102

Answers (2)

msc msc
msc msc

Reputation: 75

I was playing around with my own question and I figured it out. All I had to do is add one line of syntax to my validate_credentials() function.

The below syntax is part of my validate_credentials() function.

    if($this->session->userdata('redirect')){
         // This is the line I was missing
         $this->session->set_userdata($data);

         redirect($this->session->userdata('redirect'));

    } else {

         $this->session->set_userdata($data);

         redirect('members/members_area',$data);
    }

This is my entire validate_credentials() function.

  function validate_credentials(){
      $this->load->model('Members_data');
      $query = $this->Members_data->validate_users();
       if($query){
          $id = $this->Members_data->get_member_ID($this->input->post('username'));
          $email = $this->Members_data->get_member_email($this->input->post('username'));
          $data = array(
             'username' => $this->input->post('username'),
             'id' => $id,
             'email' => $email,
             'is_logged_in' => TRUE
           );

            if($this->session->userdata('redirect')){

                 $this->session->set_userdata($data);

                 redirect($this->session->userdata('redirect'));

            } else {

                 $this->session->set_userdata($data);

                 redirect('members/members_area',$data);
            } 

       } else {
          $this->login();
       }
    }

The below syntax is the syntax I would add to any of my "restricted" pages.

$current_url = current_url();

$this->session->set_userdata('redirect',$current_url); 

Upvotes: 0

kittycat
kittycat

Reputation: 15045

redirect('members/members_area',$data); is wrong.

The 2nd parameter is allowing you to choose between 'location' or 'refresh' redirect HTTP header. You are passing you data to it which of course will not work. You should be storing the data in the session instead if you want it to be available on the next page, which you are already doing. Change that line to:

redirect('members/members_area'); and it should now redirect.

Upvotes: 2

Related Questions