Yii Framework Forum: Ajax and session timeout - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Ajax and session timeout Rate Topic: -----

#1 User is offline   Jay_69 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 27-August 10

Posted 29 August 2010 - 07:20 PM

The case: User does nothing but watching the page. After session has expired he makes an Ajax request. But Yii does nothing. It's not redirecting to the login page why?. Any ideas? Where to control it?
The request makes jQuery function: $('#item_id').load('?r=user/ajaxaction');
Thank you all in advance.
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 29 August 2010 - 07:26 PM

There are many topics about it.
0

#3 User is offline   Jay_69 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 27-August 10

Posted 29 August 2010 - 09:12 PM

View PostY!!, on 29 August 2010 - 07:26 PM, said:

There are many topics about it.


Any link anyway concerning Yii?
0

#4 User is offline   Jay_69 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 27-August 10

Posted 01 September 2010 - 12:55 PM

Have solved by myself using callback of jQuery '.load()' function and 'responseText' parameter. :D
0

#5 User is offline   Jay_69 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 27-August 10

Posted 09 March 2011 - 12:24 PM

Still many people meet the problem when the DIV is loaded with loginPage instead of redirecting to it. I could not see any elegant solution. If anyone know please share.
That is the reason why I prefer to use my own JavaScript functions based on jQuery load function.

Here is the sample:
PHP Yii code
public function actionAjaxAction(){
   if(isset($_REQUEST['data']) && Yii::app()->request->isAjaxRequest) {

  ...your code...
  
  }
  if(Yii::app()->user->isGuest) {echo '0';} else {echo '1';}
}


JavaScript code
function ajaxCommand(){

...your code...

  $('#ajax_port_id').load('?r=controller/ajaxaction',{data:some_data,YII_CSRF_TOKEN:document.getElementsByName('YII_CSRF_TOKEN')[0].value},function(responseText){
	 if(responseText!='1') {
            $('#ajax_port_id').text('');//better clear of loginUrl code...
	    alert('Your session is timed out!'); 
	    window.location='?r=site/index';//or wherever...
	} else {
            ...your code...  
           $.fn.yiiListView.update('some_view_id'); //for example..     
        }
        $('#ajax_port_id').text('');
  });
}


HTML page should have <div id="ajax_port_id" style="display:none;"></div> and YII_CSRF_TOKEN hidden field.

What happens if the session timed out? The controller will redirect the user to the loginUrl and its content will be loaded into responseText (0 will be added to it also :) ),
then alert() then redirect...

This solution seems a little bit ugly but reliable enough.

But does anyone have the better way? Especially for Yii builtin ajax calls?
Thank you in advance.
0

#6 User is offline   zdenekca 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-May 12

Posted 01 August 2012 - 09:45 PM

View PostJay_69, on 09 March 2011 - 12:24 PM, said:


This solution seems a little bit ugly but reliable enough.

But does anyone have the better way? Especially for Yii builtin ajax calls?
Thank you in advance.


Here is a good "official" Yii solution:
http://www.yiiframew...ession-timeout/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users