4
I am building a login system and in it I am making some validations. Everything 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 is to check if the user who is trying to log in is already logged in to some session.
What I want is to prevent the same 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();
}
}
I recommend using a helper or a hook. This method of yours will only do this validation within his own class, and the ideal is that it is global, or is it not? From what I see there, he is checking if there is already a session in the bank, and if it already exists, he prints a message and closes the execution. So your problem is not to check if there is a session, but to know what to do after checking, correct?
– ShutUpMagda
Actually my problem is to check, because it is not functional. Because it is preventing a login, of a user who is not even logged in. I want to do the following: my user has ID 1, if this ID is already logged in to another browser, host, etc., then it prevents the login from being done, otherwise it allows the login to be done. I’m looking for the return
ja_logado
by ajax and in case of success I redirect the page by ajax.– Wagner Fillio
The cat jump is timestamp. To use database data you would have to validate the lifetime of this session with a timestamp. You can kill the SESSION when the browser close, but in DB it’s different. If the user logs in and then doesn’t depress, the data continues (persists) in DB, and vc will have false positives. Then I’ll give you an answer here, but you should try something like this...
– ShutUpMagda
I tried an example, but it wasn’t timestamp and it didn’t work.
– Wagner Fillio