Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ JavaScript _ idleTimeout - Prośba o sprawdzenie

Napisany przez: topcio 18.03.2018, 13:42:26

Wielka prośba, aby pomóc mi znaleźć błąd w poniższym skrypcie
Problem polega na tym, że jak odblokuje interfejs i odświeżę stronę to mnie wylogowuje, dziwne bo skoro hasło zostało przyjęte po ID sesji i user_ID to nie powinno mnie wylogować.
Jest tu jeszcze jeden bubel, ale nie ma to w tej chwili znaczenia, otóż muszę ajax request zrobić, aby zmienna zapisująca id bieżącej sesji była zapisywana w mysql jako json array, aby potem przy odblokowywaniu usuwać tylko jedno id z arraya a nie opróżniać cały, tak by działało to w kliku domenach jednocześnie.

[JAVASCRIPT] pobierz, plaintext
  1. (function($) {
  2.  
  3. $.fn.idleTimeout = function(userRuntimeConfig) {
  4. //## Public Configuration Variables
  5. var defaultConfig = {
  6. redirectUrl: '/logout', // redirect to this url on logout. Set to "redirectUrl: false" to disable redirect
  7.  
  8. // idle settings
  9. idleTimeLimit: 180, // 'No activity' time limit in seconds. 1200 = 20 Minutes
  10. idleCheckHeartbeat: 1, // Frequency to check for idle timeouts in seconds
  11.  
  12. // optional custom callback to perform before logout
  13. customCallback: false, // set to false for no customCallback
  14. // customCallback: function () { // define optional custom js function
  15. // perform custom action before logout
  16. // },
  17.  
  18. // configure which activity events to detect
  19. // http://www.quirksmode.org/dom/events/
  20. // https://developer.mozilla.org/en-US/docs/Web/Reference/Events
  21. activityEvents: 'click keypress scroll wheel mousewheel mousemove', // separate each event with a space
  22.  
  23. // warning dialog box configuration
  24. enableDialog: true, // set to false for logout without warning dialog
  25. dialogDisplayLimit: 180, // Time to display the warning dialog before logout (and optional callback) in seconds. 180 = 3 Minutes
  26. dialogTitle: 'Session Expiration Warning', // also displays on browser title bar
  27. dialogText: 'Because you have been inactive, your session is about to expire.',
  28. dialogTimeRemaining: 'Time remaining',
  29. dialogStayLoggedInButton: 'Stay Logged In',
  30. dialogLogOutNowButton: 'Log Out Now',
  31. userInfo: 'Z Powodu Zbyt Długiej Nieaktywności,<br>Konto Zostało Zablokowane.',
  32.  
  33. // error message if https://github.com/marcuswestin/store.js not enabled
  34. errorAlertMessage: 'Please disable "Private Mode", or upgrade to a modern browser. Or perhaps a dependent file missing. Please see: https://github.com/marcuswestin/store.js,
  35.  
  36. // server-side session keep-alive timer
  37. sessionKeepAliveTimer: 60, // ping the server at this interval in seconds. 600 = 10 Minutes. Set to false to disable pings
  38. sessionKeepAliveUrl: window.location.href // set URL to ping - does not apply if sessionKeepAliveTimer: false
  39. },
  40.  
  41. //## Private Variables
  42. currentConfig = $.extend(defaultConfig, userRuntimeConfig), // merge default and user runtime configuration
  43. origTitle = document.title, // save original browser title
  44. activityDetector,
  45. startKeepSessionAlive, stopKeepSessionAlive, keepSession, keepAlivePing, // session keep alive
  46. idleTimer, remainingTimer, checkIdleTimeout, checkIdleTimeoutLoop, startIdleTimer, stopIdleTimer, // idle timer
  47. openWarningDialog, dialogTimer, checkDialogTimeout, startDialogTimer, stopDialogTimer, isDialogOpen, destroyWarningDialog, countdownDisplay, // warning dialog
  48. logoutUser;
  49. //## Public Functions
  50. // trigger a manual user logout
  51. // use this code snippet on your site's Logout button: $.fn.idleTimeout().logout();
  52. this.logout = function() {
  53. store.set('idleTimerLoggedOut', true);
  54. };
  55. //## Private Functions
  56.  
  57. //----------- KEEP SESSION ALIVE FUNCTIONS --------------//
  58. startKeepSessionAlive = function() {
  59.  
  60. keepSession = function() {
  61. $.get(currentConfig.sessionKeepAliveUrl);
  62. startKeepSessionAlive();
  63. };
  64. keepAlivePing = setTimeout(keepSession, (currentConfig.sessionKeepAliveTimer * 1000));
  65. };
  66.  
  67. stopKeepSessionAlive = function() {
  68. clearTimeout(keepAlivePing);
  69. };
  70. //----------- ACTIVITY DETECTION FUNCTION --------------//
  71. activityDetector = function() {
  72. $('body').on(currentConfig.activityEvents, function() {
  73. if (!currentConfig.enableDialog || (currentConfig.enableDialog && isDialogOpen() !== true)) {
  74. startIdleTimer();
  75. }
  76. });
  77. };
  78. //----------- IDLE TIMER FUNCTIONS --------------//
  79. checkIdleTimeout = function() {
  80. var timeIdleTimeout = (store.get('idleTimerLastActivity') + (currentConfig.idleTimeLimit * 1000));
  81. if ($.now() > timeIdleTimeout) {
  82. if (!currentConfig.enableDialog) { // warning dialog is disabled
  83. logoutUser(); // immediately log out user when user is idle for idleTimeLimit
  84. } else if (currentConfig.enableDialog && isDialogOpen() !== true) {
  85. openWarningDialog();
  86. startDialogTimer(); // start timing the warning dialog
  87. }
  88. } else if (store.get('idleTimerLoggedOut') === true) { //a 'manual' user logout?
  89. logoutUser();
  90. } else {
  91. if (currentConfig.enableDialog && isDialogOpen() === true) {
  92. destroyWarningDialog();
  93. stopDialogTimer();
  94. }
  95. }
  96. };
  97. startIdleTimer = function() {
  98. stopIdleTimer();
  99. store.set('idleTimerLastActivity', $.now());
  100. checkIdleTimeoutLoop();
  101. };
  102.  
  103. checkIdleTimeoutLoop = function() {
  104. checkIdleTimeout();
  105. idleTimer = setTimeout(checkIdleTimeoutLoop, (currentConfig.idleCheckHeartbeat * 1000));
  106. };
  107.  
  108. stopIdleTimer = function() {
  109. clearTimeout(idleTimer);
  110. };
[JAVASCRIPT] pobierz, plaintext



końcówka skryptu
[php]
//----------- WARNING DIALOG FUNCTIONS --------------//
openWarningDialog = function() {

$('#blockUI').fadeIn(500);
$('#user_info').html(currentConfig.userInfo);
$.post('../php_function/user/user.php', {
ajax_test: "user_session_frozen"
});
$('#unblock_UI_confirm').click(function() {
passVerification(this);
});
console.log(timeIdleTimeout);
// var dialogContent = "<div id='idletimer_warning_dialog'><p>" + currentConfig.dialogText + "</p><p style='display:inline'>" + currentConfig.dialogTimeRemaining + ": <div style='display:inline' id='countdownDisplay'></div></p></div>";
/* $(dialogContent).dialog({
buttons: [{
text: currentConfig.dialogStayLoggedInButton,
click: function () {
destroyWarningDialog();
stopDialogTimer();
startIdleTimer();
}
},
{
text: currentConfig.dialogLogOutNowButton,
click: function () {
logoutUser();
}
}
],
closeOnEscape: false,
modal: true,
title: currentConfig.dialogTitle,
open: function () {
$(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
}
}); */
countdownDisplay();
document.title = currentConfig.dialogTitle;
if (currentConfig.sessionKeepAliveTimer) {
stopKeepSessionAlive();
}
};
passVerification = function(elem) {
event.preventDefault();
event.stopPropagation();
$.post('../php_function/user/user.php', $(elem).parents("form").serializeArray(),
function(ret) {
if (ret == true) {
$.post('../php_function/user/user.php', {
ajax_test: "user_session_unfrozen"
});
console.log(dialogTimer);
stopDialogTimer();
startIdleTimer();
destroyWarningDialog();
}
if (ret == false) {}
console.log(ret);
startIdleTimer();
console.log(startIdleTimer());
});
};
checkDialogTimeout = function() {
var timeDialogTimeout = (store.get('idleTimerLastActivity') + (currentConfig.idleTimeLimit * 1000) + (currentConfig.dialogDisplayLimit * 1000));

if (($.now() > timeDialogTimeout) || (store.get('idleTimerLoggedOut') === true)) {
logoutUser();
}
};

startDialogTimer = function() {
dialogTimer = setInterval(checkDialogTimeout, (currentConfig.idleCheckHeartbeat * 1000));
};
stopDialogTimer = function() {
clearInterval(dialogTimer);
clearInterval(remainingTimer);
};
isDialogOpen = function() {
// var dialogOpen = $("#idletimer_warning_dialog").is(":visible");
var dialogOpen = $('#blockUI').css('display') == 'block';
if (dialogOpen === true) {
$(document).on('keydown', function(e) {
if (e.keyCode == 9 || e.which == 9) e.preventDefault();
if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault();
})
return true;
}
$(document).unbind('keydown');
return false;
};
destroyWarningDialog = function() {
$('#blockUI').fadeOut(500);
document.title = origTitle;
if (currentConfig.sessionKeepAliveTimer) {
startKeepSessionAlive();
}
};
countdownDisplay = function() {
var dialogDisplaySeconds = currentConfig.dialogDisplayLimit,
mins, secs;
remainingTimer = setInterval(function() {
mins = Math.floor(dialogDisplaySeconds / 60); // minutes
if (mins < 10) {
mins = '0' + mins;
}
secs = dialogDisplaySeconds - (mins * 60); // seconds
if (secs < 10) {
secs = '0' + secs;
}
$('#countdownDisplay').html(mins + ':' + secs);
dialogDisplaySeconds -= 1;
}, 1000);
};
//----------- LOGOUT USER FUNCTION --------------//
logoutUser = function() {
store.set('idleTimerLoggedOut', true);
if (currentConfig.sessionKeepAliveTimer) {
stopKeepSessionAlive();
}
if (currentConfig.customCallback) {
currentConfig.customCallback();
}
if (currentConfig.redirectUrl) {
window.location.href = currentConfig.redirectUrl;
}
};
return this.each(function() {
if (store.enabled) {
store.set('idleTimerLastActivity', $.now());
store.set('idleTimerLoggedOut', false);
activityDetector();
if (currentConfig.sessionKeepAliveTimer) {
startKeepSessionAlive();
}
startIdleTimer();
} else {
alert(currentConfig.errorAlertMessage);
}
});
};
}(jQuery));
[php]

Niestety nie mogę tej końcówki umieścić w tagach, bo tekst jest za długi smile.gif


Problem prawdopodobnie leży w startDialogTimer i stopDialogTimer, bo w konsoli licznik się nie zeruje, ale nie potrafię tego ugryźć.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)