Return to login screen when session expires

7 posts by 3 authors in: Forums > CMS Builder
Last Post: October 13, 2019   (RSS)

Hello,

I am using the auto-expire setting for a user's login session under Admin >Security Settings as this is really important for my website.

Would it be possible to automatically redirect the user to the login page when their login session expires?

The reason I ask is because I am using DataTables and if the user tries to search the DataTable when their login session has expired then an error is thrown. But the user isn't to know that the error has been caused by the fact that their session has expired. If they were auto redirected to the login screen then I think this would improve the user flow...

Thanks,

Greg

Hi Greg,

Could you provide a little more info?

  • Are these users logged into the front- or back-end of your site?
  • Is the situation such that if they search with DataTables they get an error, but if they refresh the page they'll be returned to the login screen?

Thanks,

Daniel
Technical Lead
interactivetools.com

Hi Daniel,

Thanks for the message.

These are frontend pages of my website that are using the website membership plugin.

When the user refreshes the page they will be returned to the login screen.

Thanks again,

Greg

Hi Greg,

Generally, the expiry is only checked on page load, so to do this without a refreshing will require some custom scripting to periodically check the user's login status.

The approach I'd take would be to create a PHP file that checks if the user is logged in, and outputs a simple value (e.g. 0 or 1) depending on the result. Then add some JavaScript to the header that uses jQuery's $.ajax() method (https://www.w3schools.com/jquery/ajax_ajax.asp) to call the PHP script and check which value it returns, and refresh the page if necessary. The setInterval() method (https://www.w3schools.com/jsref/met_win_setinterval.asp) can be used to continually repeat the AJAX call.

If you need any hands-on help setting this up feel free to send us an estimate request (https://www.interactivetools.com/estimate/) - otherwise I'm happy to answer any more specific questions you have.

Thanks!

Daniel
Technical Lead
interactivetools.com

By gversion - October 1, 2019

Hi Daniel,

That's really excellent advice, thank you very much.

I will see if I can get this working myself and if not then be in touch with you guys again!

Regards,

Greg

By kitsguru - October 11, 2019 - edited: October 11, 2019

A session expires at the set interval after the browser is closed. An interval of 30 minutes means 30 minutes after quitting the browser. An interval of 0 means as soon as the browser is closed. Reopening the browser to the site within the interval, resets the timer.

you can override this by using javascript ajax to delete the session cookie and unset the PHP session variable.

Jeff Shields