jbonnett Posted August 10, 2015 Share Posted August 10, 2015 (edited) Hi All, I'm trying to use ajax to essentially get a page without changing anything already on the current page, so a modal shows after 29 mins and gives me the option to stop the user from logging out by clicking a button. The php session controls the logout by checking the session timestamp vs the one in the database, if the session one is older than 30 mins with no activity e.g. viewing a page... then logout. The problem is to "view" a page I use an ajax call (although it still logs the user out): $.ajax({ cache: false, type: "GET", dataType: 'html', url: "<?=$config["pre_link"].$_SERVER["REQUEST_URI"]?>", success: function(data) { }, error: function(){ }, complete: function(){ } }); Here is the whole thing if you need it: $(document).ready(function() { var timeout = setTimeout(function() { $("#logout_warning").modal('show'); }, 29*60*1000); var end = new Date().getTime(); function resetTimeout() { clearTimeout(timeout); timeout = setTimeout(function() { $("#logout_warning").modal('show'); }, 29*60*1000); end = new Date().getTime(); } $("#stop_logout").click(function() { resetTimeout(); $("#logout_warning").modal('hide'); $("#logout_warning .modal-content").css("display", "none"); $.ajax({ cache: false, type: "GET", dataType: 'html', url: "<?=$config["pre_link"].$_SERVER["REQUEST_URI"]?>", success: function(data) { }, error: function(){ }, complete: function(){ } }); }); $("#stop_logout .close").click(function() { $("#logout_warning").modal('hide'); $("#logout_warning .modal-content").css("display", "none"); }); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date().getTime(); var distance = ((end+30*60*1000) - now) - 1000; if (distance < 0) { clearInterval(timer); window.location.reload(true); return; } var days = Math.floor(distance / _day); var hours = Math.floor((distance % _day) / _hour); var minutes = Math.floor((distance % _hour) / _minute); var seconds = Math.floor((distance % _minute) / _second); //$("#logout_mins").html(minutes); $("#logout_secs").html(seconds); } timer = setInterval(showRemaining, 1000); }); Edited August 10, 2015 by jbonnett Quote Link to comment https://forums.phpfreaks.com/topic/297705-using-ajax-to-retain-login/ Share on other sites More sharing options...
scootstah Posted August 13, 2015 Share Posted August 13, 2015 I don't understand the problem. If the user is active, then the session will get refreshed every time they view a page, no? Is that not what you want to happen? Quote Link to comment https://forums.phpfreaks.com/topic/297705-using-ajax-to-retain-login/#findComment-1518709 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.