检查用户是否登录 laravel 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20696813/
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
Check if user is logged in laravel 4
提问by user1424508
Hi I am implementing a chat/inbox feature on my website. When users are not logged in and receive a message from another user, the website will send an email to the recipient of the message. However when the user is logged in it won't.
嗨,我正在我的网站上实施聊天/收件箱功能。当用户未登录并收到来自其他用户的消息时,网站将向消息接收者发送电子邮件。但是,当用户登录时,它不会。
My questions is how do I tell whether a user is logged in or not? For example, if a user is logged in and then closes the browser my backend wouldn't know anything.
我的问题是如何判断用户是否登录?例如,如果用户登录然后关闭浏览器,我的后端将一无所知。
Btw I'm building a Angularjs SPA with a restful API in Laravel 4.?
顺便说一句,我正在 Laravel 4 中构建一个带有宁静 API 的 Angularjs SPA。?
回答by Anam
if you are using build-in Laravel Auth:
如果您使用内置 Laravel Auth:
if(Auth::check())
{
//do something
}
if you are using Sentry:
如果您使用的是哨兵:
if(Sentry::check())
{
//do something
}
回答by AturSams
I suggest using Ajax to pingthe DB when the user is logged in. Add a last_logged_in
column of type Date
So when the users are logging in they update their last_logged_in
field to the current date. You can do this Ajax as often as you like (very minimal performance issues). To see if a user is logged in simply check the last_logged_in
date and compare it to the current date minus some time (1 minutes)..
我建议在用户登录时使用 Ajax ping数据库。添加一个last_logged_in
类型为Date
So的列,当用户登录时,他们将其last_logged_in
字段更新为当前日期。您可以随心所欲地执行此 Ajax(极少的性能问题)。要查看用户是否登录,只需检查last_logged_in
日期并将其与当前日期减去一些时间(1 分钟)进行比较。
I would not suggest using it to email users because it may result in lots of spam.
You can use the is_logged_in
field in addition but don't use it on it's own.
我不建议使用它给用户发送电子邮件,因为它可能会导致大量垃圾邮件。您可以is_logged_in
另外使用该字段,但不要单独使用它。