This solution requires Yii 1.1.9 or above
The problem:
A user has no activity for a longer period of time and the php session times out. After the session has expired he makes an Ajax request and nothing happens or, in case of a grid view, the grid completely disappears.
Expected behavior:
Show the login page and redirect after successful login
The solution:
Set 'loginRequiredAjaxResponse' in your configuration (config/main.php)
... 'components'=>array( 'user'=>array( ... 'loginRequiredAjaxResponse' => 'YII_LOGIN_REQUIRED', ... ), ...
In your view or template
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 = options.url; } } ); '); }
Note:
The code above is tested with a grid view where options.url is a useable page url. If this is not the case or you just want to show the login page with no redirect afterwards you can replace
window.location.href = options.url;
with
window.location.href = "'.Yii::app()->createUrl('/site/login').'"
Total 3 comments
Be mindful that using options.url will redirect you to the AJAX call(obviously). This might not be what you when using a CHtml::ajaxButton() and an actionAjax() since - for example - you'll only see a JSON response. To fix this and go back to the proper page I used the following:
As for pre 1.9 releases, you'll need to add the following code to WebUser:
This worked for my AJAX buttons, I haven't tested it within list/grid views.
Using ajaxComplete is wrong, since the Ajax call may itself be using this, possibly leading to errors and thus failure to redirect. The redirect must happen before ajaxComplete.
ajaxSuccess is the correct event to use.
oh i found it is a new var introduced in yii 1.9 ! useful wiki ,by the way in ajax mode user should not be bring to another page to login ^-^ ; may be :
this is a js code ,so we can do anything to replacing it ; we can use some lightBox to load a login form and do ajax login ,after success just reload the gridview/listview data
Leave a comment
Please login to leave your comment.