php - 在 codeigniter 中查找用户的总登录时间

标签 php codeigniter

我必须在 codeigniter 中准备用户登录和注销时间的报告。我已经覆盖了 session 库并根据它更改了 set_userdata 的功能。

我的问题是当用户没有选择注销按钮,或者他/她同时从多个系统登录时;在那种情况下,我如何记录登录和注销时间?

我在session库的session_destroy函数中添加了如下代码

$session_id=$this->userdata['session_id']; 
$c_array=array('logout_time'=>date('Y-m-d H:i:s')); 
$this->CI->db->where('session_id',$session_id); 
$this->CI->db->update('login_activities',$c_array);

但是 ci_session 表中的 session_id 在一段时间后发生了变化

最佳答案

我们不能总是获取注销时间,因为如果用户关闭浏览器将无法添加注销时间,在正常注销的情况下就可以了..

我的建议是:

我们需要 current_time 和一个usage field

而不是 logout_time

如果用户登录,则每秒或(可能在 3 秒或 5 秒内)在页面上运行 ajax

每次 ajax 运行时:current_time 更新为当前时间戳,usage 字段必须更新为以秒为单位的时间,可以通过 login_time 和当前时间的差值计算

使用以下函数:

function getTimeInSec($t1, $t2)
{
    $timeFirst  = strtotime($t1);
    $timeSecond = strtotime($t2);
    $differenceInSeconds = $timeSecond - $timeFirst;
    return $differenceInSeconds;
}

这里 $t1 是当前时间,$t2 是登录时间.... (两者都是格式 'Y-m-d H:i:s')

ajax调用脚本:

<script>
jQuery(document).ready(function($){
    getRefresh(1000);
});
function getHeader()
{
     $.post("<?php echo site_url('mycontroller/refreshAjax');?>", {
 },
 function(response)
 {
 });
}
function getRefresh(tim)
{
    getHeader();
    setTimeout("getRefresh("+tim+")", tim);
}
</script>

在 Controller 函数 refreshAjax() 中必须有更新表的更新查询...

关于php - 在 codeigniter 中查找用户的总登录时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20964233/

相关文章:

php - 在 prestashop 中手动更新产品数量(外部脚本)

php - Laravel 7 : Why isn't my session cookie getting set in a browser?

php echo 如果两个条件都为真

php - 是否使用 MySQL 中的当前/默认字符集转换 BLOB?

php - MySQL "less than"无法正常工作

javascript - 网站从http迁移到https

php - WAMP 上的 Mysqli,错误 - 连接尝试失败

javascript - ng-click 在 Angular js 中显示数据

jQuery GET 不工作

php - 通过 AJAX、PHP 和 MYSQL 传递参数时返回多个值