Redirect with timer and user usage identifier

Asked

Viewed 999 times

0

My doubt is a little complex:

Today I use a simple redirect just using HTML:

<meta HTTP-EQUIV='Refresh' CONTENT='480;URL=./logout.php?type=2'>

Where "CONTENT" is the regressive time (in seconds) it will count before redirecting. But I use this code as an auto-logout. As 480 seconds is 8 minutes, after 8 minutes without a page change or update the system will "depress" automatically. However, I had to add a new Eature in the system, where it is now normal to exceed that time.

What I really need is something (whether in Javascript/ajax/PHP/html/Jquery) to reset the time if the user is moving the mouse or keyboard (or interacting with the page) so that the system does not close in the middle of work.

In my head this is a bug, if anyone can help me... I really appreciate it!

  • 1

    I think the ideal is for you to do this control on the server-- invalidate the session after a certain downtime.

  • But doing this will not continue with the same problem no?

  • Please clarify the phrase "where now is normal to exceed that time"

  • Ops Pablo, it was a Portuguese error of mine: "where now it is normal to exceed this time" actually is: "where now it is normal to exceed this time" (already corrected) if your doubt is about the Feature I added, it is a text editor, and this made the use on the same page much bigger causing several users to lose the text, since the system closed only

1 answer

1


I don’t know how you control the validity time of the session on the server, so I’ll focus exclusively on the client.

The basic idea is to use the function 'setTimeout(Function, timeEmMiliseconds)' to "schedule" the call of the function 'logoutFunction'. In this function I use the time of the last interaction with the system to decide if the user will be redirected to the logout page or if the redirect will be delayed.

To save the last modification time a function ('setLastModified') was created that simply modifies the variable used in the time comparison ('var lastModified'), then the function is assigned to the 'onmousemove' and 'onkeypress' html events, so every time there is a mouse move or a key is pressed the time will be updated.

(function(){
    var minutesToLogout = 8 * 60 * 1000; // Configure o tempo que achar melhor
    var logoutUrl = './logout.php?type=2'; // Configure a URL de logout

    var lastModified = Date.now();
    var html = document.getElementsByTagName('html')[0];
    var setLastModified = function(){lastModified = Date.now();}

    html.onmousemove = setLastModified;
    html.onkeypress = setLastModified;

    var logoutFunction = function(){
        var timeDiff = Date.now() - lastModified;
        if(timeDiff > minutesToLogout)
            document.location = logoutUrl;
        else
            setTimeout(logoutFunction, minutesToLogout - timeDiff);
    }
    setTimeout(logoutFunction, minutesToLogout);
})();

Note. The setTimeout function is not exact, it may have a small delay in some circumstances, but nothing very worrying.

  • Dude, Erik, thank you so much, I’ll keep trying because the code you passed isn’t working 100% when I’m filling out the form it doesn’t detect the keypress, but it worked perfectly for the mouse. I’m not very good at JS(I don’t really know anything about JS), I may have got mixed up in something but it has already served to direct... Vlw even!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.