更新时间:2023-02-05 20:09:58
决定将什么放入会话数据以及现在有效地归结为一种权衡——性能与存储空间.显然,这些论点对于小项目来说基本上是无关紧要的,只会在用户、流量等高的情况下产生影响.
Deciding what to put in session data and what now effectively comes down to a trade-off – performance vs. storage space. Obviously these arguments are basically irrelevant for tiny projects, and only make a difference with high amounts of users, traffic, etc.
首先,如果您的登录是基于会话的,像往常一样,PHP 会在需要时加载所需的会话数据,即在请求中提供会话 cookie 时.会话数据必须至少包含登录用户的用户名/ID.
First of all, if your login is session-based, as is usual, PHP is going to load the required session data when needed, that is, when a session cookie is provided in the request. The session data has to contain, at the very least, the username / id of the logged-in user.
为什么不在会话数据中放入更多?每当用户登录时,将数据库中的数据加载到会话中.
Why not put a lot more into the session data? Whenever the user logs in, load data from the database into the session.
在这种情况下,您只需在会话数据中保留 ID 或用户名.然后根据需要从数据库加载任何其他数据.
In this case, you only keep the id or username in the session data. Any other data is then loaded as needed from the database.