Check if userid is logged in (session - codeigniter)

3

I'm building a login system and I'm doing some validation on it. All functional except, the verification if user is already logged in.

I'm using the method below, but it's not 100% functional. What I want to do is check if the user trying to login is already logged in to some session.

What I want is to prevent a single user from having two sessions.

Method Code:

private function _valida_usuario_logado()
{
    $id = $this->session->userdata('id');
    $ci_sessions = $this->auditoria->listar_ci_sessions();//$this->db->get('tbl_ci_sessions');      

    foreach($ci_sessions->result() as $item)
    {           
        $user_data = $item->user_data;
        $sessions = unserialize($user_data);

        $id_logado = $sessions['id'];           
    }

    if ($id == $id_logado)
    {           
        echo 'ja_logado';           
        exit();
    }
    else
    {
        echo 'nao_logado';
        exit();
    }
}
    
asked by anonymous 06.09.2017 / 04:01

2 answers

0

In each control, I use the following form:

if($this->session->userdata('logado')==null) redirect('login');

In this case, logged in, is the name of the session you directed. So, the build would look like this:

class Interno extends MY_Controller {

    public function __construct(){
        parent::__construct();
        if($this->session->userdata('logado')==null) redirect('login');
        ...
    }
}
    
06.09.2017 / 17:29
0

Wagner, I do not think there's any way to do it, I've tried it as well.

What can be done and works more or less cool, is to create a table in the database that makes a mark when logging, because the sessions are individual by browser, then at the time of logging in, check if you are already logged in bank.

BUT, it may happen that the user just closes the browser instead of logging off, hence could not log in anymore.

THEN I think the most correct would be to do as GOOGLE does, just warn the user who was fetus a login at date and time x, whether or not that login is recognized. If it says yes, it deletes the last login and creates a new one.

    
06.09.2017 / 19:00