I am trying to redirect my website to the homepage when the session expires and the user is automatically logged out (right now it gets redirected to the joomla core registration page, which I don't want the user to see because I have created my own registration page in a custom component).

In the index.php file of my template, I check if the session is active and perform a redirect if the session expired:

$session = JFactory::getSession();

if (!$session->isActive()) {
   echo "session is expired";   
   header('Location: http://www.example.com/');
   echo "session is active";

This solution worked fine up until the moment I updated to Joomla 3.4.5. After the update, even when the user is logged out automatically, it behaves as if the session is still active (I check that through the echo "session is active" line, as you can see above).

Why is the session still active even if the user is logged out ? Is there an alternative to the $session->isActive() method ?


If you look at the session.php file, you see the following:

public function isActive()
    return (bool) ($this->_state == 'active');

Which is the function you're calling. I'm not sure if this was done purposely or if it was a mistake, but it will always return 1.

Instead, you use:

$session = JFactory::getSession();
echo $session->getState();

Which will return one if the following:

  • inactive
  • active
  • expired
  • destroyed
  • error

You can then base your if statement on this, for example:

$session = JFactory::getSession();

if ($session->getState() !== 'active') 
    echo 'session is not active';   
    header('Location: http://www.example.com/');
    echo 'session is active';

On a side note, I'd suggest using Joomla JFactory::getApplication()->redirect() method instead of header()

Hope this helps

  • Thanks so much !! This is exactly what I was looking for. It worked perfectly ! Thanks again ! :) – user3716433 Dec 7 '15 at 17:13
  • @user3716433 - You're welcome. Please mark the answer as accepted by clicking the "tick" icon – Lodder Dec 7 '15 at 17:16
  • return (bool) ($this->_state == 'active'); will not always return 1, it returns "true" if $this->_state is "active", otherwise it returns false. I don't see why your code works and the initial code not!? Just curious... $session->getState() !== 'active' produces the same results like !$session->isActive() – fruppel Dec 8 '15 at 8:10
  • @fruppel - I've just tested this function on a Joomla 3.4.5 site and it's always returning 1, even thought it's supposed to return a boolean – Lodder Dec 8 '15 at 8:36
  • Ok thx I'll check it too. Maybe it's a bug – fruppel Dec 8 '15 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.