jeff5656 Posted February 11, 2012 Share Posted February 11, 2012 I have a form that is very long. Sometimes the logged-in user will spend over 20 minutes on this screen and then when they hit submit, the session has already expired and they are redirected to the login screen and anything on that form is lost. Is there a way to reset the session timeout to zero everytime they either click the screen with the mouse, or type something into a form field (without submitting the form)? Not sure if this would only work with javascript or if there is php code that I would put in the beginning of the action.php that looks for whether the session has timed out and if so, add the form contents to the database first before logging them out? A php solution like that would be preferable to a javascript one. But also if the sessiopn has already expired, then the action.php script would be less secure if I put the code there because then anyone not logged in could use that page so that's probably not the solution. Best solution would be to reset the session timer whenever they click ont he screen or start typing in the form. Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/ Share on other sites More sharing options...
scootstah Posted February 11, 2012 Share Posted February 11, 2012 You can't do it with PHP because PHP doesn't interface with the client. But you can run an AJAX request every X minutes. With jQuery: $(function(){ // set interval for 5 minutes setInterval(function(){ $.ajax({url: "keep-alive.php"}); }, 300000); }); Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/#findComment-1317055 Share on other sites More sharing options...
PFMaBiSmAd Posted February 12, 2012 Share Posted February 12, 2012 You could just set session.gc_maxlifetime to a longer reasonable value. The purpose of session.gc_maxlifetime is to delete old session data files, not to prevent users from actually using your pages that rely on session variables. Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/#findComment-1317209 Share on other sites More sharing options...
scootstah Posted February 12, 2012 Share Posted February 12, 2012 In some cases a short session life is more desirable. Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/#findComment-1317211 Share on other sites More sharing options...
jeff5656 Posted February 12, 2012 Author Share Posted February 12, 2012 You could just set session.gc_maxlifetime to a longer reasonable value. The purpose of session.gc_maxlifetime is to delete old session data files, not to prevent users from actually using your pages that rely on session variables. No this won't solve the problem. If that longer time expires and the user finally presses submit, the for data will not get processed because the user's session will have expired and they will just getr redirected to the login screen. Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/#findComment-1317295 Share on other sites More sharing options...
jeff5656 Posted February 12, 2012 Author Share Posted February 12, 2012 You can't do it with PHP because PHP doesn't interface with the client. But you can run an AJAX request every X minutes. With jQuery: $(function(){ // set interval for 5 minutes setInterval(function(){ $.ajax({url: "keep-alive.php"}); }, 300000); }); Where do I put that code? I put jquery in the jquery folder. do I put this in the body? <script type="text/javascript"> $(function(){ // set interval for 5 minutes setInterval(function(){ $.ajax({url: "keep-alive.php"}); }, 300000); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/#findComment-1317307 Share on other sites More sharing options...
scootstah Posted February 13, 2012 Share Posted February 13, 2012 Normally it goes in the <head>. Quote Link to comment https://forums.phpfreaks.com/topic/256905-reset-session-timeout-without-submitting-form/#findComment-1317429 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.