0

However there are lots of ways in PHP to logout the application when user is idle using session i am using

while login

$_SESSION['last_activity']=time()+10;

in header

$expire_time = 10; //10 secs
if($_SESSION['last_activity'] < time()-$expire_time) {
    echo 'session destroyed';
}
else {
    $_SESSION['last_activity'] = time();
}

this function logs out the user based on user clicks or refreshing the page even on tabs, but not mouse events which is possible in javascript

var IDLE_TIMEOUT = 900; //seconds
var _idleSecondsCounter = 0;

document.onclick = function () {
    _idleSecondsCounter = 0;
};

document.onmousemove = function () {
    _idleSecondsCounter = 0;
};

document.onkeypress = function () {
    _idleSecondsCounter = 0;
};

window.setInterval(CheckIdleTime, 1000);

function CheckIdleTime() {
    _idleSecondsCounter++;
    var oPanel = document.getElementById("SecondsUntilExpire");
    if (oPanel)
        oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
    if (_idleSecondsCounter >= IDLE_TIMEOUT) {
       alert('Times up!, You are idle for about 15 minutes, Please login to continue');
        document.location.href = "logout.php";
    }
}

but this wont work across the project or tabs, if user keeps a tab idle and works on another tab the whole project would get logged out, is there any way to make this script works globally or make php to detect all events.

1 Answers1

1

You may use JavaScript local storage API

With local storage, web applications can store data locally within the user's browser.

Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.

var IDLE_TIMEOUT = 900; //seconds
    sessionStorage.idleSecondCounter =  0;

document.onclick = function () {
    sessionStorage.idleSecondCounter = 0;
};

document.onmousemove = function () {
    sessionStorage.idleSecondCounter = 0;
};

document.onkeypress = function () {
    sessionStorage.idleSecondCounter = 0;
};

window.setInterval(CheckIdleTime, 1000);

function CheckIdleTime() {
    sessionStorage.idleSecondCounter = parseInt(sessionStorage.idleSecondCounter)+1;
    var oPanel = document.getElementById("SecondsUntilExpire");
    if (oPanel)
        oPanel.innerHTML = (IDLE_TIMEOUT - sessionStorage.idleSecondCounter) + "";
    if (sessionStorage.idleSecondCounter >= IDLE_TIMEOUT) {
       alert('Times up!, You are idle for about 15 minutes, Please login to continue');
        document.location.href = "logout.php";
    }
}

The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.

  • try `localStorage.setItem('idleSecondCounter', 0);` instead of 'sessionStorage.idleSecondCounter' –  Jul 10 '17 at 09:24
  • and plz tell me that which browser are u using –  Jul 10 '17 at 09:24
  • and i forgot to tell that u must have this uploaded to a server or network then only it will work. it won't work cross tabs if u have not uploaded to a server –  Jul 10 '17 at 09:26
  • thanx for responding, actually i tried localStorage too its working, but not perfectly, if i place 10 seconds and working on some tab and jump to second tab i is not logging out working fine, but if i left both the tabs and went to some other page on browser its logging out within 2 or 3 seconds. im using chrome latest version – Pattatharasu Nataraj Jul 10 '17 at 10:13
  • that can't be it.... how about sending me your full project in an compiled zip file –  Jul 10 '17 at 15:01
  • 1
    I figure out when u have 2 tabs open... Their are 2 scripts acting to subtract time –  Jul 11 '17 at 09:29
  • https://stackoverflow.com/questions/45012558/localstorage-on-javascript-leads-refreshing-probelm i got solution here, thanx yaar. – Pattatharasu Nataraj Jul 11 '17 at 09:34
  • actually thas the problem, both the scripts acting, if i use time it was working perfectly – Pattatharasu Nataraj Jul 11 '17 at 09:35