销毁 Code Igniter 中的特定会话 [英] Destroying a specific session in Code Igniter

查看:39
本文介绍了销毁 Code Igniter 中的特定会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够让用户退出我的 Code Igniter 内置应用程序.

I want to be able to log users out of my app built in Code Igniter.

我知道如何结束活动的本地会话:

I know how to end an active local session:

$this->session->sess_destroy();

但是我怎样才能销毁在另一台计算机上启动的会话,从而使用户退出他们的会话?

But how can I destroy a session that was started on another computer and thereby log a user out of their session?

我在会话数据中存储了一个与他们的帐户相关联的唯一 ID,因此我可以在数据库的会话表中看到它,但它与其他会话数据一起存储在名为 user_data 的列中,其中的内容看起来像这样:

I store a unique id associated with their account in the session data, so I can see it in the session table in the database, but it is stored along with the other session data in a column called user_data, the contents of which look something like this:

a:4:
{s:9:"user_data";s:0:"";s:6:"userid";s:6:"189034";s:9:"logged_in";b:1;s:5:"token";i:1767727789;}

其中 189034 是用户的 ID.

where 189034 is the user's id.

那么,有没有办法根据用户的 id 以某种方式选择会话表中的行,然后删除该行并销毁会话.还是有另一种方法可以完全做到这一点?

So, is there a way to somehow select the row in the session table based on the user's id, and then delete the row and destroy the session. Or is there another way to do this entirely?

推荐答案

在 ci_session 表中创建一个新列.

Create a new column in the ci_session table.

ALTER TABLE `yourdatabase`.`ci_sessions` 
ADD COLUMN `userid` VARCHAR(45) NULL  AFTER `user_data` ;

然后在您的登录函数中从您的登录过程中获取 id,并在将用户数据信息添加到会话之前执行:

Then in your login function get the id from your login process and before adding the userdata information to the session do:

/* Delete any existing sessions with the current userid session.
Note - a new session has already been generated but doesn't have a value
in the userid column, so won't get deleted. */
$this->db->delete('ci_sessions',array('userid' => $identity));    

// Get the current session and update it with the userid value.
$session_id = $this->session->userdata('session_id');
$this->db->where('session_id', $session_id);
$this->db->update('ci_sessions', array('userid' => $identity));

其中 $identity 是您的用户 ID.这会清除之前的所有会话,意味着一次只存在一个.

Where $identity is your userid. That clears any previous sessions and means only one exists at a time.

这篇关于销毁 Code Igniter 中的特定会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆