如何知道是否有人通过 javascript 登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5450024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:13:54  来源:igfitidea点击:

how to know if someone is logged via javascript?

javascriptjquery

提问by karim79

in php i would do

在 php 我会做

if (isset($_COOKIE['user']) && $_COOKIE['user'] == $valueFromDb)
 echo 'logged';

But in javascript how can I check it?

但是在 javascript 中我如何检查它?

Thanks

谢谢

回答by karim79

There is no reliable way to do this without making a request to the server, since the session might have expired on the server side. In one project, I do it the following, imperfect way:

如果不向服务器发出请求,就没有可靠的方法来执行此操作,因为会话可能已在服务器端过期。在一个项目中,我采用以下不完美的方式:

checkAuth() {
    var logged = (function() {
        var isLogged = null;
        $.ajax({
            'async': false,
            'global': false,
            'url': '/user/isLogged/',
            'success': function(resp) {
                isLogged = (resp === "1");
            }
        });
        return isLogged;
    })();
    return logged;
}

I say imperfect because it makes a synchronous request which temporarily locks up the browser. It does have an advantage in that checkAuth()can be reliably called from any part of the client-side application since it forces the browser to wait for the result before continuing execution of the JS code, but that's about it.

我说不完美是因为它发出了一个同步请求,暂时锁定了浏览器。它确实有一个优点,checkAuth()可以从客户端应用程序的任何部分可靠地调用,因为它强制浏览器在继续执行 JS 代码之前等待结果,但仅此而已。

The alternative, asynchronous (non-locking) approach would be to do something like this:

另一种异步(非锁定)方法是执行以下操作:

$.ajax({
    'url': '/user/isLogged/',
    'success': function(resp) {
        if(resp === "1") {
            // do your thing here, no need
            // for async: false  
        }
    }
});

回答by Arturo Igualada

When you want to know if user is logged on a page to do some type of special action, I usually use a hidden input with a special name that is checked on js. If the user is logged you put that on a master page or something like that and then in js check if it exist to know if the user is logged.

当您想知道用户是否登录页面执行某种类型的特殊操作时,我通常使用在 js 上检查的具有特殊名称的隐藏输入。如果用户已登录,则将其放在母版页或类似内容上,然后在 js 中检查它是否存在以了解用户是否已登录。

Something like:

就像是:

<input type="hidden" class="UserIsLogged" />
<script type="text/javascript">
        if ($(".UserIsLogged").length > 0) {
            //User is logged
        }
</script>

回答by bmargulies

You are mixing up server-side processing (php) with client side processing (javascript). On the client side this information does not exist unless the server has provided it in some framework-specific way.

您将服务器端处理 (php) 与客户端处理 (javascript) 混为一谈。在客户端,除非服务器以某种特定于框架的方式提供此信息,否则此信息不存在。

回答by Mohammed Swillam

Yes. check out this question: Create, read, and erase cookies with jQuery

是的。查看这个问题:使用 jQuery 创建、读取和删除 cookie

a plugin to make it even easier for you: http://plugins.jquery.com/project/cookie

一个让你更轻松的插件:http: //plugins.jquery.com/project/cookie