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

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

问题描述

工作人员注意 :此问题和相关答案已锁定,以防止主题讨论当前活动。有关此活动的问题 可以在我们的元网站上找到。非常感谢!


我希望能够将用户从我在代码Igniter中构建的应用中注销。



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

  $ this-> session-> ; sess_destroy(); 

但是如何销毁在另一台计算机上启动的会话,会话?



我在会话数据中存储了与其帐户相关联的唯一ID,所以我可以在数据库的会话表中看到它,其他会话数据在名为user_data的列中,其内容如下所示:

  a:4:
:s:9:user_data; s:0:; s:6:userid; s:6:189034s:9:logged_in token; i:1767727789;}

其中 189034 是用户的id。



所以,有一种方法,以某种方式根据用户的id选择会话表中的行,然后删除该行并销毁会话。

解决方案

在ci_session表中创建一个新列。

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

然后在您的登录函数中从您的登录过程中获取id,然后将userdata信息添加到会话do:

  //删除当前用户ID会话的任何现有会话。 
//注意 - 已经生成了一个新会话,但在userid列中没有值
//,因此不会被删除。
$ this-> db-> delete('ci_sessions',array('userid'=> $ identity));

//获取当前会话并使用userid值更新它。
$ session_id = $ this-> session-> userdata('session_id');
$ this-> db->其中('session_id',$ session_id);
$ this-> db-> update('ci_sessions',array('userid'=> $ identity));

其中$ identity是您的用户标识。这会清除之前的任何会话,并且每次只能有一个会话。


Staff Note: This question and the associated answers are locked to prevent off topic discussion surrounding a current event that is related to the question at hand. Questions about this event can be found on our meta site. Thank you!

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?

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;}

where 189034 is the user's 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?

解决方案

Create a new column in the ci_session table.

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

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));

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

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

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