简体   繁体   中英

Manage Joomla session timeout

I have a custom component which has a simple form and I'm struggling how to manage session timeouts. If a user clicks away another link they will get redirected automatically by Joomla. They get redirected to the login component and upon successful login they go to the page they last clicked. This is exactly what I want.

However the problem is with the form. If a user times out and doesn't realize so they fill out this form and hit submit they get left on the same page with a default session timeout message. Why doesn't it redirect to the login component?

I guess I need to handle this case in my code, so when my form is submitted it calls this function in my controller:

function process()
{
   $user = JFactory::getUser();     

    if ($user->id)
    {
        //process form
    }
    else //give warning and bring them to a page upon successful login
    {
        JError::raiseWarning( 100, JText::_('You have timed out. Please login again.') );
        JFactory::getApplication()->redirect(JRoute::_('my-account'));
    }

}

But that doesn't work and I get the same Joomla default session timeout message. I also tried to get the session instead of checking user but that didn't work either.

$session = JFactory::getSession();
if ($session->isActive()) 
{
    //process form
}
else
{
    JError::raiseWarning( 100, JText::_('You have timed out. Please login again.') );
    JFactory::getApplication()->redirect(JRoute::_('my-account'));

}

Any ideas?

You can keep the form from timing out by adding the following code to the form HTML:

<?php
  JHTML::_('behavior.keepalive');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM