views:

397

answers:

2

Hi all,

I have a list of "online users", which I refresh every 30 seconds with Ajax.PeriodicalUpdater from prototype.js.

There is an issue, that if a user has two tabs/windows open with the site, logs out in one of them, the PeriodicalUpdater is not canceled.

This means that PeriodicalUpdater updates my block element with the entire login page, as I have a restful authentication, that redirects to that.

I am using PHP sessions, and I really cannot get my head straight on this one.

Can any of you guys point me in the right direction?

Thanks!

/ preben

A: 

When creating the PE, keep a reference to it:

// At global scope, or ideally if you have a namespaced object, use that
var thePE;

// Where you create the PE
thePE = new PeriodicalExecuter(yourFunction, 3);

And when you want it to stop, tell it to stop (assuming it has been started):

if (thePE) {
    thePE.stop();
    thePE = undefined;
}

(PeriodicalExecuter#stop docs here.)

T.J. Crowder
Thanks for the tip! I am trying to figure out, where and when I actually want it to stop. Is there any way I can check if my PHP session is still active in the JavaScript?
Preben
A: 

I would consider changing the method that returns the results so that it doesn't require an authenticated session to access it, but it returns valid information only when there is an authenticated session. Otherwise, it returns a value indicating that the updates should cease and a suitable message for display in your online user's block. This would mean that you'd probably need to return JSON instead -- not a bad thing -- and either combine it with an HTML snippet as part of the JSON or simply construct the HTML on the client from the JSON data (better IMO since the view is the correct place to do mark up).

tvanfosson
Of course! Man, you're a star. I already have a list of excluded files in my access control class.I added onlineList.php to my $excludedPages array, wrapped the entire page in if(isset($_SESSION['user_id'])) - everything is good.Thank you very much!
Preben