Destroy (Logout) Session_ID Codeigniter

4

Through the code below I retrieve all users logged into 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 drop user for each line displayed, which when clicked, the user is logged out.

Using 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 I logged off ?

    
asked by anonymous 24.01.2017 / 19:30

1 answer

3

You need to create a method and exclude the record from this user by session_id of 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');   
}

Next user action will be directed to log in again, this prevents him from working in the last session, but not in creating a new one, this can also be done with a status or active field %.

24.01.2017 / 19:47