Recommended way to let javascript know about php server-side login/logout?

By : Kzqai

For convenient notification purposes, not secure information purposes:

I have javascript polling for new information while logged in. I want it to stop while logged out. What is the best way to let javascript know about the logged-in/logged out status?

Run a javascript function on login, and one on logout (which might get skipped if they navigate away and come back directly)? Run a poll periodically to check for access to secure (logged-in-only) information and skip further polls if that access isn't available? Another option that I haven't found yet?

I'm running php server-side, if matters.

By : Kzqai


Ok, here is what I decided to do:

  • Keep a boolean that corresponds to
    logged in/logged out state (1 or 0).
  • Login toggles 1.
  • Logout toggles 0.
  • If the existing polling checks the api and gets false return values because a logout has occurred, it will toggle the boolean to 0 as well (after either 1 or two false information checks).
  • Once the boolean is 0, no further polling will occur until a login event toggles it back to 1.
By : Kzqai

You could just set a Javascript boolean at page load time: true if the user's logged in, and false if they're not.

You can add a class="loggedin" or id="loggedin" to the

By : vdboor

This video can help you solving your question :)
By: admin