Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent session expired in PHP Session for inactive user

Tags:

I have a problem with my application: my application has many forms and need about 1 hour to finish this form because the form is dynamic (can add other forms). The problem is: the session of my web server is 24 minutes. When user fill the form, they spent so much time and the session timed out because server recognize that the user is inactive. It's very annoying when form submitted, most data was lost and the user is returned to the login page. I have tried to make my session expired in 10 hours with this code:

ini_set('session.gc_maxlifetime', '36000'); 

But it's not working in my server, is it possible my server preventing ini_set() function?

So, what should I do for solving this problem? Can I prevent session timeout so that the session can be expanded to 10 hours? Or can I disable session expiration?

Thanks

like image 307
dian Avatar asked May 11 '11 10:05

dian


People also ask

How can solve session expired in PHP?

How can solve session expired in PHP? if you want to expire the session after 30 minutes of activity instead of after 30 minutes since start, you'll also need to use setcookie with an expire of time()+60*30 to keep the session cookie active.

How do I start a PHP session?

Starting a PHP Session A PHP session is easily started by making a call to the session_start() function. This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.

Why session has expired?

If your Internet connection is unstable, periodically disconnecting and reconnecting, it can cause a website session to expire. When the Internet connection is lost the website connection can be terminated, resulting in a session expired message if you try to access any page after the Internet reconnects.


2 Answers

Instead of setting the time in ini to a fixed length, remind that session timeout is reset on reload. So create some ajax code that does a request every 5 minutes or so to a file (image or smth). This way the timer is reset every 5 minutes and users can spend a day filling out your forms.

like image 126
Prikkeldraad Avatar answered Sep 22 '22 18:09

Prikkeldraad


Here an example to prevent session timeout by using a jQuery Ajax call:

var refreshTime = 600000; // every 10 minutes in milliseconds window.setInterval( function() {     $.ajax({         cache: false,         type: "GET",         url: "refreshSession.php",         success: function(data) {         }     }); }, refreshTime ); 

in the refreshSession.php you can do something like session_start()

like image 35
MDeuerlein Avatar answered Sep 21 '22 18:09

MDeuerlein