Destroy(Logout) Session_id Codeigniter

Asked

Viewed 436 times

4

Through the code below I recover all users logged in to the system and display them in a table.

<?php
        $dados = array(
            'session_id',
            'ip_address',
            'last_activity',
            'user_data'
        );
        $this->db->select($dados);
        $query = $this->db->get("ci_sessions");        
?>

<tbody>
  <?php foreach ($query->result() as $linha):?>
     <tr>
     <?php
    $sessao = $linha->user_data;
    $dados = unserialize($sessao);
     ?>
     <?php if(!empty($dados['nome_usuario'])): ?>
     <td><?php echo htmlspecialchars($dados['nome_usuario'],ENT_QUOTES, 'UTF-8');?></td>
     <td><?php echo htmlspecialchars($linha->ip_address, ENT_QUOTES, 'UTF-8'); ?></td>
     <td><?php echo date('d/m/Y H:i:s',$linha->last_activity);?></td>
     <td><a href="<?php echo site_url('login/deslogar'); ?>" 
          class="btn btn-danger btn-flat">
     <?php echo lang('usuarios_deslogar_usuario'); ?></a></td>
     <?php endif; ?>
     </tr>
  <?php endforeach;?>                                   
</tbody>

What I want now, is to have an option to depress user for each line displayed, that when clicked, user is deprecated.

Through the button:

<td>
  <a href="<?php echo site_url('login/deslogar'); ?>" 
          class="btn btn-danger btn-flat"><?php echo 'Deslogar'; ?>
  </a>
</td>
<?php endif; ?>

So how do I destroy only the session of id that dropped?

  • I already did that, and did passing the session_id and removing this line from the database, so when this user tried to do any other action on the site he was redirected to the login screen

1 answer

2


You need to create a method and delete this user’s record by session_id table ci_sessions:

Html

<td>
  <a href="/login/deslogar/<?php echo $linha->session_id;?>" 
          class="btn btn-danger btn-flat">Deslogar</a>
</td>

Controller

public function deslogar($id)
{
    $this->db->where('session_id', $id);
    $this->db->delete('ci_sessions');   
}

The next action of the user, the same will be directed to login again, this prevents him to work in the last session, but, not in the creation of a new, this can be done also with a field status or active.

References

  • 1

    Thank you! , it worked for me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.