Skip to content

Commit

Permalink
Merge branch 'fix/34' into 'release/2.3'
Browse files Browse the repository at this point in the history
Fix/34 重写当前会话活动功能 #34

See merge request imunc/iplacard!104
  • Loading branch information
fengkaijia committed Jun 8, 2019
2 parents 6956a79 + 1a77bc0 commit 3ad6f57
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
17 changes: 13 additions & 4 deletions application/controllers/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ function activity()

$this->load->helper('ip');
$this->load->helper('date');
$this->load->helper('session');

//获取记录
$this->db->where('operator', uid());
Expand All @@ -1104,11 +1105,19 @@ function activity()

if($query->num_rows() != 0)
{
$data = array();
$sessions = array();

foreach($query->result_array() as $data)
{
//检查是否仍然在线
$data['value'] = json_decode($data['value'], true);

//自CI 3.0起在同一浏览器上的多次登录可被登记为同一Session
if(in_array($data['value']['session'], $sessions))
continue;
else
$sessions[] = $data['value']['session'];

//检查是否仍然在线
$this->db->where('session', $data['value']['session']);
$check = $this->db->get('session');

Expand All @@ -1134,8 +1143,8 @@ function activity()
$data['current'] = true;

//是否已经关闭
$user_data = session_decode($session['data']);
if(!isset($user_data['halt']) || !$user_data['halt'])
$user_data = session_unserialize($session['data']);
if(isset($user_data['logged_in']) && $user_data['logged_in'])
$active[] = $data;
}
}
Expand Down
38 changes: 38 additions & 0 deletions application/helpers/session_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Session辅助函数
* @author Kaijia Feng <[email protected]>
* @copyright 2019 Kaijia Feng
* @license Dual-licensed proprietary
* @link http://iplacard.com/
* @package iPlacard
* @since 2.3
*/

/**
* 解析CodeIgniter序列化Session为数组
* @link https://www.php.net/manual/en/function.session-decode.php#108037
* @author Frits van Campen <[email protected]>
* @param string $session 序列化Session字符串
* @return array Session数组
*/
function session_unserialize($session)
{
$return = array();
$offset = 0;
while($offset < strlen($session))
{
$pos = strpos($session, '|', $offset);
$num = $pos - $offset;
$varname = substr($session, $offset, $num);
$offset += $num + 1;
$data = unserialize(substr($session, $offset));
$return[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return;
}

/* End of file session_helper.php */
/* Location: ./application/helpers/session_helper.php */

0 comments on commit 3ad6f57

Please sign in to comment.