0

I wanted to log users out of the back office after 15 minutes of inactivity.

With my code, the disconnection works but also if I am active.

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

Any tips?

cabrerahector
  • 3,653
  • 4
  • 16
  • 27
microb14
  • 453
  • 4
  • 16

1 Answers1

1

Should should use jQuery to detect idl time. Check this: How to detect idle time in JavaScript elegantly?

Then you use Ajax to load you PHP script.

So you will have something like that:

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

And in your logout.php just call the function to logout actuel user

Thomas LIBERATO
  • 313
  • 2
  • 5