i want to do something in jquery, if the user wants to vote on something, and they are not logged in or registered, 开发者_StackOverflow中文版you prompt them to do something just like stackoverflow, when the orange pop up thingy comes up, but i was wondering how you work with jquery and sessions!! i just want a push in the right direction! :))
You need to set a javascript variable in your code somewhere that says whether or not the user is logged in. This can be from the backend sessions or from a browser cookie that you set. You definetly don't want this to be the only measure against voting if they are not logged in though. Check it on the server as well.
If you want to use sessions, you can do something like this:
<script type="text/javascript">
var userLoggedIn = <?php =$_SESSION['logged_in']; ?>
</script>
$_SESSION['logged_in']
would have to be set to either true
or false
. Then you just check that variable (userLoggedIn
) every time the user wants to perform an action. Also check it on the server-side as I said above.
SO seems to use this method. If you look in the code, there is a line var isRegistered = true
, which means that I am currently logged in.
Another way you could do it is to remove all voting functionality if they are not logged in, and add it when they are. This would save on load time for those who aren't logged in.
Without knowing what your setup is (i.e. if you're using a framework, etc) it's hard to give the best possible answer; however, in general terms you'd just have to do the following:
- Check if the session is set and
- If it is, launch the javascript popup.
Assuming the login session info is stored in a slot called 'username', you would do:
if (!isset($_SESSION['username'])) {
print("
<script type type=\"text/javascript\">
alert('Please login first.');
</script>
");
}
I recommend looking into jQuery UI for great ways to make things look more appealing.
Sessions are managed on the server and have nothing to do with jQuery.
You can use jQuery to make nice modal dialogs for login screens, but that's just fancy HTML.
精彩评论