#Yii ajax session timeout

It’s been a long I didn’t write about something “technically”. Maybe because I didn’t get in touch again with those technical stuff. Ok then what I’m gonna to write is about ajax session timeout in Yii framework. Sometimes this problem is never counted since in Yii session timeout has been handled in the config. However, the config is only handle HTTP request not include ajax request.

So, how to deal with the ajax request when the session is already timeout?
First add this line to your main config (usually located at protected/config/main.php)

'components'=>array(
    'user'=>array(
        'loginRequiredAjaxResponse' => 'YII_LOGIN_REQUIRED',
    ),

Then at this code to the layout view

<!--?php 	if (Yii::app()--->components['user']->loginRequiredAjaxResponse){
        Yii::app()->clientScript->registerScript('ajaxLoginRequired', '
            jQuery("body").ajaxComplete(
                function(event, request, options) {
                    if (request.responseText == "'.Yii::app()->components['user']->loginRequiredAjaxResponse.'") {
                        window.location.href = "'.Yii::app()->createUrl('/auth/index').'";
                    }
                }
            );
        ');
    }
?>

Source :
http://www.yiiframework.com/wiki/321/using-loginrequiredajaxresponse-to-solve-ajax-session-timeout

*) miss the code😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s