Welcome Guest, Not a member yet? Register   Sign In
Login validation session qeustion
#1

[eluser]ThatsJustMe[/eluser]
Hello,

What would be the best choice of making sessions after I have validated login data? Well basically, I need to set people as online, after he has passed the login validation, and so he doesn't need to relogin after moving to another page or reloading page. In normal php I do it with sessions, but in codeigniter sessions are stored in database so I'm a little bit confused.

this is my current script, please suggest what I should update -

Code:
public function logout() {
    $this->load->helper(array('form', 'url'));    
    $this->session->unset_userdata('logged_in');
    $this->session->unset_userdata('username');
    $this->session->unset_userdata('ip_address');
    $this->session->sess_destroy();            
    redirect('/home', 'refresh');
  }
  
  // Do validation after registrate button is pressed.
  public function validate() {
    validation stuff goes here....
    
    // If doesn't pass validation, redirect's back with errors.
  if ($this->form_validation->run() == FALSE)
  {
      $this->load->helper('url');  
      $this->load->view('includes/header');
    $this->load->view('login');
    $this->load->view('includes/footer');
  }
  // If everything is correct logins.
  else
  {
      $login = array(
         'ip_address'    => $this->input->ip_address(),
         'username'  => $this->input->post('uname'),
         'logged_in' => TRUE
      );
      
      $this->session->set_userdata($login);  
      redirect('/profile','refresh');  
  }
  }


Well basically currently it sets sessions, but it deosn't logout. Could you give me some hints, what I'm doing wrong?
#2

[eluser]oliur[/eluser]
To allow user to logout you need to give user an option so that can trigger your logout function. In other word, create a logout link which should link to your logout() function.



#3

[eluser]ThatsJustMe[/eluser]
Hello,

Okay, I write the link to logout function, it redirects me to home but it doesn't unset session, why so?
#4

[eluser]oliur[/eluser]
I want you to do a quick test:

1) check if you are loading session library into the library array in your config file

2) once you set session echo username, It's just to see session has been set correctly

$username = $this->session->userdata('username');
echo $username;


3) On logout rather than using individual variables unset the entire array

$this->session->unset_userdata($login);


4) Now try to read the username again.

BTW, It works for me.

#5

[eluser]ThatsJustMe[/eluser]
Oh yeah!

It works lol, my bad, defined wrong attribute.

Thanks Wink!




Theme © iAndrew 2016 - Forum software by © MyBB