Control of idle time in different windows php + java script

0

I have been investigating about the closing of sessions due to inactivity in my page, I have achieved it with the following code in my main page (index.php):

script

<script>
function idleLogout() {
    var t;
    window.onload = resetTimer;
    window.onmousemove = resetTimer;
    window.onmousedown = resetTimer; // catches touchscreen presses
    window.onclick = resetTimer;     // catches touchpad clicks
    window.onscroll = resetTimer;    // catches scrolling with arrow keys
    window.onkeypress = resetTimer;

    function logout() {
        window.location.href = 'logout.php';
    }

    function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout,140000);  // time is in milliseconds
    }
}
idleLogout();
</script>

and I pass the function in body to index.php

<body onunload="idleLogout()"> 

logout.php (I destroy the session)

<?php
        session_start();
        session_destroy();
        header( "Location:session_expire.php" );
 ?>

But my index.php has features that open in NEW WINDOWS, then, when I stop interacting in index.html because I'm in those windows, what to do to have this session time reset, since close my session

    
asked by IndiraRivas 09.05.2018 в 21:27
source

1 answer

0

What you could do, is, when some time passes, check if the browser window (your index.php in this case) is active or not; it would be something like that

var window_focus;

$(window).focus(function() {
//Retorna true si index.php está en foco
    window_focus = true;
})
    .blur(function() {
//false en caso contrario
        window_focus = false;
    });

You could check it in the following way;

var window_focus;

$(window).focus(function() {
    window_focus = true;
})
    .blur(function() {
        window_focus = false;
    });

  $(document).one('click',function() {
    setInterval(function() { console.log('en foco? ' + window_focus); }, 1000);
    });
Click para empezar<br>

<script src="https://code.jquery.com/jquery-3.3.1.slim.js"
  integrity="sha256-fNXJFIlca05BIO2Y5zh1xrShK3ME+/lYZ0j+ChxX2DA="
  crossorigin="anonymous"></script>

Base the answer on a user113716 at StackOverflow in English

Greetings!

    
answered by 09.05.2018 в 22:02