I would like to determine whether or not the user is logged in or if they're just anonymous from javascript...
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.
Anyone know how I can check to see if the user is logged in from javascript, possibly using ajax?
Edit: I'm running Asp.Net MVC, sorry should have specified
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
You can't read session saved data from JavaScript.
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>';
}
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>