数据库中的用户表已更改,如何更新会话? [英] User table in database is changed, how to update session?

查看:68
本文介绍了数据库中的用户表已更改,如何更新会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当用户登录我的网站时,将启动(PHP)会话并将其数据库行保存在$ _session中.之所以这样做,是因为目前用户数据的大小很小,因此不必每次都需要向数据库询问用户数据.

Currently when the user logs in my site, a (PHP) session is started and his database row is saved in his $_session. I do so because the size of user's data is small for now, and this way I do not have to ask the database for user's data every time I need it.

问题是,当我想在开发过程中添加或更改某些内容并触摸数据库时,会话不会以这种方式更新.用户必须注销并再次登录才能更新会话.由于该站点处于开发阶段,所以现在这样做是好的,但是在生产中这是不可取的.

The problem is that, when I want to add or change something during the development and I touch the database, the session is not updated this way. The user have to logout and login again to update the session. This is good for now since the site is in development phase, but in production this is not desirable.

我不会删除会话文件,因为人们很懒,我会避免每次数据库发生更改时都强迫他们重新登录,并且我不想每X分钟重新加载一次会话.现在,我的解决方案是在用户表中创建一个布尔列,默认情况下为false,在更改数据库时将其设置为true.每次会话继续进行时,如果该值变为true,则会话数据将被更新.无论如何,每次用户继续会话时,我都必须执行一个(小)查询.我不知道这在生产中是否会成为问题.

I wouldn't delete session files, because people are lazy and I would avoid to force them to re-login every time something change in database, and I do not want to reload the session every X minutes. My solution for now is a boolean column inside the user's table, false by default, that I set true when I change the database. Every time a session is continued, session data will be updated if that value becomes true. Anyway this way I have to do a (small) query every time an user continues a session. I do not know if this will be a problem in production.

是否有替代/更好的方法来解决我的问题?

Is there an alternative / better way to solve my problem?

推荐答案

如果我正确理解了这个问题,一种解决方法是,每次更新数据库中的用户行时,也可以更新用户的会话数据在PHP中.

If I understand the problem correctly, one way to handle this would be every time a user row in the DB is updated that user's session data could be updated as well in PHP.

如果不是用户更新记录(例如管理员更改用户的权限),则您很可能希望强制注销该用户.如果是用户更新记录(例如更改用户配置文件中的信息),则只需更新会话变量中的值就足够了.

If it is not the user updating the records (such as an administrator changing a user's permissions) most likely you would want to force a logout of the user. If it is the user updating the records (such as changing information in their user profile) simply updating those values in the session variables may be enough.

在这两种情况下,您可能还希望向用户提供一条消息,让他们知道发生了什么.

In both of these cases you probably also want to provide a message to the user letting them know what happened.

这篇关于数据库中的用户表已更改,如何更新会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆