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

For convenient notification, unprotected informational purposes:

I have a javascript poll to get new information during login. I want him to stop while logging out. What is the best way to tell javascript about login / logout status?

Run the javascript function when you log in and one when you log out (which may be skipped if they are moved and returned directly)? Run the survey periodically to check access to protected (login only) information and to skip additional polls if this access is not available? Another option I haven't found yet?

I use php server-side if that matters.

+3
source share
6 answers

Cookie approach should work. Another thing you can do is create a JSON page that returns true or false if the user is logged in. Then check that before your other code, if it is logged in, do what you do, if you don’t do anything else, maybe redirect to the Login page?

In jQuery, it looks something like this:

$.getJSON("/login/is_logged", function(json) {
    if(!json.User.logged) {
        window.location = '/login/form/';
    }
});

The JSON page (url / login / is_logged) returns this:

{"User":{"logged":true}}

Here is a link to $. getJSON .

+4
source

Set a cookie for login / logout actions: for example. logged=1:)

+2
source

javascript , , .

+2

Javascript boolean : true, , false, .

+1

class="loggedin" id="loggedin" <body> <html> DOM/jQuery.

HTML document.documentElement.className.

PHP , , , "logged in" <script> .

+1

, :

  • , (1 0).
  • Login 1.
  • Logout 0.
  • If an existing poll checks the api and receives false return values ​​because it logged off, it will switch the logical value to 0 as well (after either 1 or two invalid data checks).
  • As soon as the logical value is 0, further polling will occur until the login event switches it to 1.
0
source

Source: https://habr.com/ru/post/1723633/


All Articles