How to redirect to the login page after a certain idle PHP time?

Asked

Viewed 574 times

4

As the title of the question says I would like at a certain time where the user is idle, and so the system destroys the session and automatically redirects to the login page. I know php has the session_cache_expire function() (http://php.net/manual/en/function.session-cache-expire.php). However, from what I understood this time is independent of the user being idle, besides that at the end of the time the session will only be destroyed when the page is requested. I believe that many here have had the unpleasant experience of filling out an extensive form and when sending the Ubmit lose data due to the time spent filling out the form (I had filling in the data for ENEM registration on mobile). I would prefer to use only PHP, but I think it will be necessary to use javascrpit as well. Thanks for your attention.

  • 1

    This example of Enem is dirty programming (and nut programming is what more has, especially in the public thing). A decent system would keep the form data, even the finished session, and allow you to relocate to continue, in a more extreme case. There may be some fool who doesn’t understand PHP and will tell you to increase session time, but would only change the problem of place (besides not being the right way to use session). The ideal is to think about how to store this form, regardless of the session, and allow the user to continue where he left off (this is true for any kind of data, not just for this case).

  • 1

    In other words, it’s great that you’re worrying about it, but there’s no magic recipe that solves it simply. Session is supposed to be an auxiliary tool. Everything that needs to take a long time, can be managed in more complex ways. For example, it is much cleaner to automatically and transparently relocate the user than to shorten the session. In this case the individual cookie and the non-destructive login (accept the form anyway, but ask login to proceed) has less side effects.

  • 1

    About using JS, one way would be for a script to keep renewing the session with PHP, but if in the meantime there is a small connection fault, you might have the problem the same way. Another idea would be to use JS in addition to solving the problem with PHP, you have a local cache of the form filled (and discard after a reasonable time without use, of course, whether in JS or PHP). Whatever path you choose, it will take a little reasoning and whim, but after all, programming is exactly that.

1 answer

1

I’d do it this way,

1 - User logged in (create a temporary table in db, record a record containing the date and time)

2 - A javascript is started in order to perform get requests to this time table by checking the date and time record in it and comparing the date and time of the request, this javascript must be executed in intervals of XX minutes.

3 - The user makes an action loads a page for example: (updates the record by rescheduling date and time) - Javascript continues its routine

4 - The user does not perform action for a while - javascript at some point will confront the data and give yes, when the comparison time is longer than stipulated (idle time allowed), the javascript triggered a php that will destroy the temporary table and send the user the login screen again.

I use this way in a system I developed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.