【问题标题】:Automatically log out user from WordPress dashboard after 15 minutes of inactivity不活动 15 分钟后自动从 WordPress 仪表板注销用户
【发布时间】:2020-12-14 12:38:52
【问题描述】:

我想在 15 分钟不活动后让用户退出后台。

使用我的代码,断开连接有效,但如果我处于活动状态。

function myplugin_cookie_expiration( $expiration, $user_id, $remember ) {
    return $remember ? $expiration : 900;
}
add_filter( 'auth_cookie_expiration', 'myplugin_cookie_expiration', 99, 3 );

有什么建议吗?

【问题讨论】:

标签: php wordpress


【解决方案1】:

应该使用jQuery来检测idl时间。 检查这个:How to detect idle time in JavaScript elegantly?

然后你使用 Ajax 来加载你的 PHP 脚本。

所以你会有类似的东西:

var idleTime = 0;
$(document).ready(function () {
    //Increment the idle time counter every minute.
    var idleInterval = setInterval(timerIncrement, 60000); // 1 minute

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });
});

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > 14) { // 15 minutes
        $.ajax({
            url: 'logout.php',
            success: function(data) {
                window.location.reload();
            }
        });
    }
}

在您的 logout.php 中,只需调用该函数即可注销实际用户

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-04
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2017-06-27
    • 2016-04-13
    相关资源
    最近更新 更多