Google Chrome restores session cookies after a crash, how to avoid?

前端 未结 2 653
南方客
南方客 2021-01-18 13:30

On Google Chrome (I saw this with version 35 on Windows 8.1, so far I didn\'t try other versions) when browser crashes (or you simply unplug power cable...)

2条回答
  •  不要未来只要你来
    2021-01-18 14:08

    I didn't find anything I can use as process id to be sure Chrome has not been restarted but there is a dirty workaround: if I setup a timer (let's say with an interval of five seconds) I can check how much time elapsed from last tick. If elapsed time is too long then session has been recovered and logout performed. Roughly something like this (for each page):

    var lastTickTime = new Date();
    
    setInterval(function () {
        var currentTickTime = new Date();
    
        // Difference is arbitrary and shouldn't be too small, here I suppose
        // a 5 seconds timer with a maximum delay of 10 seconds.
        if ((currentTickTime - lastTickTime) / 1000 > 10) {
            // Perform logout
        }
    
        lastTickTime = currentTickTime;
    }, 5000);
    

    Of course it's not a perfect solution (because a malicious attacker may handle this and/or disable JavaScript) but so far it's better than nothing.

    New answers with a better solution are more than welcome.

提交回复
热议问题