我如何检查是否用户是匿名还是从JavaScript登录? [英] How can I check to see if the user is anonymous or logged in from javascript?
问题描述
我想确定用户是否登录,或者如果他们只是匿名在JavaScript ...
I would like to determine whether or not the user is logged in or if they're just anonymous from javascript...
我发现这个问题,但它的PHP code和我不知道是否有一个名为LOGGED_IN一个会话变量被存储在登录,如果这只是一些人已经实施了自己。
I found this question, but it's php code and I wasn't sure if there is a session variable called logged_in that gets stored on login or if that was just something that person had implemented himself.
任何人都知道我可以检查用户是否可以从JavaScript登录,可能使用AJAX?
Anyone know how I can check to see if the user is logged in from javascript, possibly using ajax?
编辑:我跑Asp.Net MVC,对不起,应该指定
I'm running Asp.Net MVC, sorry should have specified
这是为了让我可以实现客户端AJAX登录。页面加载时,我需要知道,如果用户登录或不那么我可以实现类似于&LT东西; ASP:LoggedInView>使用jQuery的
控制
This is so that I can implement a client-side ajax login. When the page loads, I need to know if the user is logged in or not so I can implement something similar to an <asp:LoggedInView>
control using jquery.
谢谢,
马特
Thanks,
Matt
推荐答案
您无法读取会话从JavaScript保存的数据。
You can't read session saved data from JavaScript.
这是简单的方法来做到这一点是建立从PHP一个JS变种(比cookie的解决方案更容易),就像这样:
An easy way to do it is to create a JS var from PHP (easier than cookies solution), like this:
if ($_SESSION['logged_in'] == 1) {
echo '<script type="text/javascript">var logged_in=true;</script>';
} else {
echo '<script type="text/javascript">var logged_in=false;</script>';
}
然后,JS会简单地检查本机 LOGGED_IN
VAR,看看用户是否登录。
Then the JS will simply check the native logged_in
var to see if the user is logged in.
<script type="text/javascript">
if (logged_in) {
alert("My user is logged in!");
}
</script>
这篇关于我如何检查是否用户是匿名还是从JavaScript登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!