Session Timeout + AJAX + dbSession

Hi everyone, this is my first forum post. I’ve started using yii a few months ago and I am loving it!

I thought I’d make my contribution, perhaps someone is struggling with the same issue.

In my application I have a number of javascript functions that open dialogs and such.

Naturally, if the user’s session has expired I wish to prevent him to do so, and redirect him to the login page.

This may seem rather crude but my solution is the following:

My javascript function (makes an AJAX call to controller):




checkSession = function(){

                $.get(baseUrl + '/site/ajaxCheckSession','', function(data_in) {

                    if (data_in != ''){

                        // Redirect user with passed location

                        window.location = data_in;

                    }

                });

            }



The AJAX function in the controller:




    public function actionAjaxCheckSession() {

        // Create sessions object

        $sessions = new CHttpSession();

        

        // run getSessionId, this seems to trigger garbage collection...

        $sessions->getSessionId();

        

        if (count($_SESSION) == 0){

            // No session? pass location redirect to javascript!

            echo $myUrl;

        }

    }



And there you go…

Hope that is useful to someone! :)

Hi and welcome to the forum…

I would suggest you to pay attention where you post on the forum… because you posted in the Yii 1.0.x sub-forum… and as you just started with Yii I doubt you are using this "old" version…

Because this version is deprecated, not many users read this section…

As for your solution…

in the next Yii version (1.1.9) there will be a new option CWebUser::loginRequiredAjaxResponse that can be set to any message you need… this way in your JS code you can just check if the returned data is that message to know that the session has expired…

http://code.google.com/p/yii/source/detail?r=3368

ops! my bad!

thanks for the info!