Welcome Guest, Not a member yet? Register   Sign In
Codeigniter re-direct after user logs in?
#1

[eluser]scm22ri[/eluser]
Hi Everyone,

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

Below is the page I want my visitor to login to see. After they login I want them re-directed to the below page.
Code:
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

Code:
$current_url = current_url();

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

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

Code:
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.

Code:
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();
    }
}
#2

[eluser]jairoh_[/eluser]
i want to learn this one also. Smile
#3

[eluser]cyruxx[/eluser]
I think you have to set a redirect type.

Edit: Also you should really read the documentation:
URL - Redirect - Documentation

Here is an example:

Code:
redirect($this->session->userdata('redirectURL'), 'REDIRECTTYPE');
#4

[eluser]scm22ri[/eluser]
Hey guys,

Thanks for your replies. I tried a few different variations of your syntax and it still isn't working. Any other help would be appreciated. Thanks everyone.
#5

[eluser]Unknown[/eluser]
Hi there,

Your code is actually working fine. Just that if you are actually checking whether the user have already logged in using the session data, you won't be able to because you forgot to add.

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

in your if statement. So the correct code would be.

Code:
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);
}

I have tested it and it is working on my side.
#6

[eluser]scm22ri[/eluser]
Hey Furyking,

Thanks for the tip. The syntax is working great. Appreciate the help.




Theme © iAndrew 2016 - Forum software by © MyBB