Handling Session timeout


(Kuldeep Kaundal) #1

Hi,

I wanted to discuss an issue related to session logout in yii, which i feel is a common problem in many apps.

I am using CJuiTabs in my application that displays Ajax Content after the user logs into the system. The issue is that in-case the user logs out from the application from some other window or his session times out, and then clicks one of the tabs, at first instant the whole index layout is rendered as the view in the tab content area and then certainly the screen goes white, which shouldn’t happen, the user must see login screen or should be redirected to login page in case his session has timed-out.

I looked around and found some solutions here http://danaluther.blogspot.com/2010/04/loginrequired-and-ajax-updates.html

But,the thing is that suppose you logout from one window, click at tab, you see the session timeout error, however, if you login again, the tabs view is rendered distorting site layout.

Is there a way to overcome this issue? Please suggest!

Thanks,

Kul.


(Kuldeep Kaundal) #2

OK, I solved the issue by using above url like this:

class WebUser extends CWebUser

{

public function loginRequired()


{


   $app = Yii::app();


   $request = $app->getRequest();


   


   $this->setReturnUrl( $request->getUrl() );


   


   if ( $request->isAjaxRequest )


   {


      Yii::app()->user->logout();


      echo "<script>alert('you need to login to perform this action!');window.location.reload();</script>";


   } else {





      if ( ($url = $this->loginUrl) !== null )


      {


          if (is_array($url))


          { 


              $route=isset($url[0]) ? $url[0] : $app->defaultController;


              $url=$app->createUrl($route,array_splice($url,1));


          }


           $request->redirect($url);





      }


      else 


        throw new CHttpException(403, Yii::t('yii', 'Login Required'));


   }


}

}