Showing posts with label session. Show all posts
Showing posts with label session. Show all posts

Wednesday, August 8, 2012

Joomla 2.5 - Redirect to login page with return URL

If you have a private page that is supposed to be viewed only by registered users and if URL is accessed directly or is accessed on session expire you want to redirect user to login page and on successful login return him to the requested page then simply make a function named something like 'validateUser' and call it for all such views. Here is the brief and comprehensive function:


function validateUser()
{
        $user = JFactory::getUser();
        $userId = $user->get('id');
        if(!$userId)
        {
            $mainframe = &JFactory::getApplication();
            $return = JFactory::getURI()->toString();
            $url  = 'index.php?option=com_users&view=login';
            $url .= '&return='.base64_encode($return);
            $mainframe->redirect($url, JText::_('You must login first') );
            return false;
        }
        return true;
}

Note: If you are trying to do something similar for Joomla 1.5 note you would have to use 'option=com_user' instead of 'users'.

Thursday, March 24, 2011

Joomla 1.5 admin session timeout error

Yesterday I was running a script in Joomla admin from a custom developed. The script basically uploaded an xml file and entered the blogs from the xml into the site. It was working fine as look as the number of blog/entries in the file were small but when they were large enough to exceed the default joomla session time of 15 min it used to logout admin during the script and I had to start all over again.

I googled for the solution and easily found out that the way to do it is go to Joomla admin of the site then go to:
Site> Global Configuration> System
and at the right bottom you will see an option 'Session Settings' with a value of 15 minutes in 'Session Lifetime', you can simply change it to whatever you like and you are going.

However now comes the reason for writing the post. What I did was try to make this time unlimited. First I tried using '0 minutes' but I think that also uses the default value of 15 minutes because that also logged me out after some time so I tried using the maximum value by pressing and holding 9 for a while. Then I saved and I got logged out and when I tried to login it didn't let me. Yes my admin was kind of blocked as it wasn't giving me error but not letting me in. I tried entering wrong password and it gave me invalid user/pass error but nothing in case of correct user/password. Luckily it was my local site and not some production one, I looked a lot for the place where this value is saved to overwrite it manually but didn't find anything and finally after about an hour I found that it was in Joomla's root directory file configuration.php with a variable of '$lifetime' almost at the end of the file. I change it to 180 and both the issues resolved i.e. I could log back in to admin and the file was also completed in an hour or two without logging me out in between.

However as we know that session time should be kept as low as possible to avoid security breach.

Hope it helps someone.