Force log out after a period of inactivity

In order to enhance the security of the site, you may want to automatically log out your users after x minutes of inactivity on the site. This can be achieved by adding a redirect code to your system.

If a user is logged in in multiple tabs, this script will keep all tabs logged in until no movement is detected for 10 minutes in any of them, at which point they will all log out. 

Steps


1. Copy the following code, which is to auto-log out after 10 minutes of inactivity.

You can change the value shown below to your desired duration.

<script>
    (function(){
        if (typeof(Storage) !== "undefined") {

            // Browser supports local storage
            // Change this variable to modify the inactive time before logout
            var minutesInactiveToLogout = 10;
            var secondsCheckLogoutFrequency = 30;
            var doReset = false;
            
            $(document).on('ready mousemove keypress', function(){ doReset = true;});

            //Check if we should log out every x frequency
            setInterval(function(){checkForLogoutCondition();}, secondsCheckLogoutFrequency * 1000);

            function checkForLogoutCondition(){
                if (doReset) {
                    resetTimer();
                    doReset = false;
                    return;
                }
                var lastActiveTime = readLocalStorage('logOutTimer');
                if(lastActiveTime){
                    currentDate = new Date();
                    currentTime = currentDate.getTime();
                    if((currentTime - (minutesInactiveToLogout * 60 * 1000)) >= lastActiveTime){
                        //Log out
                        logOut();
                    }
                }
            }

            function resetTimer(){
                var currentDate = new Date();
                var currentTime = currentDate.getTime();
                //Overwrite the localstorage
                createLocalStorage('logOutTimer',currentTime);
            }

            function logOut(){
                var currentLocation = window.location.pathname;
                location = '/logout?page='+currentLocation;
            }

            function createLocalStorage(name,value) {
                localStorage.setItem(name, value);
            }

            function readLocalStorage(name) {
                return localStorage[name];
            }
        } else {
            //No Web Storage support..
        }
    }());
</script>

 

2. Admin > System > Script

Paste the code inside the head tag

 

3. Scroll down and click 'Save' to apply.

Now the value you set will apply to allow users, and after x minutes of inactivity, the system will log them out.

 

Created on 12 March 2026 by Hannah Door

3828 Views   

Share