php登录会话登录变量
我正在学习 php 并且已经注册了一门课程.我的用户登录信息显示在下面.
I am learning php and have enrolled in a course. my user login is displayed below.
我的问题是我是否必须在登录时在会话中选择我想要的所有字段?我可以不只是使用选择电子邮件并通过它拉出所有行,还是我必须在登录时选择所有行?
my question is do i have to select all fields i want in a session when logging in? could i not just use Select email and pull all rows by that or to i have to select all row on login?
以该会话的 uid = uid 为例,如果它不提取有关 id = id 的用户的所有信息吗?
example take this uid = uid for that session should it not pull all info about the user with id = id?
$_SESSION['email'] = $email;
$_SESSION['uid'] = $row['uid'];
function login_user($email, $password)
{
$sql = "SELECT pwd, uid, user, birthdate FROM users WHERE email = '" . escape($email) . "' AND active = 1";
$result = query($sql);
if (row_count($result) == 1) {
$row = fetch_array($result);
$db_password = $row['pwd'];
if (password_verify($password, $db_password)) {
$_SESSION['email'] = $email;
$_SESSION['uid'] = $row['uid'];
$_SESSION['username'] = $row['username'];
$_SESSION['birthdate'] = $row['birthdate'];
return true;
} else {
return false;
}
return true;
} else {
return false;
}
}
决定将什么放入会话数据以及现在有效地归结为一种权衡——性能与存储空间.显然,这些论点对于小项目来说基本上是无关紧要的,只会在用户、流量等高的情况下产生影响.
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.
- 登录后无需执行任何数据库操作,只要数据不发生变化即可.
- 减少数据库服务器上的负载.
- PHP 将数据存储在其服务器端会话存储中的效率相对较低.
- 如果您的数据库服务器与 PHP 服务器不同,这意味着您现在将数据存储在错误的服务器上.
- 如果数据发生变化,会话必须失效.如果更改会话中存储的数据的唯一方法是通过用户交互,那么这很容易被发现.如果数据可以从外部更改(即其他用户可以通过一些交互更改数据),这是一个问题——会话数据有时需要失效,因此您无论如何都必须检查数据库.
在这种情况下,您只需在会话数据中保留 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.
- 用户名/用户名通常是一成不变的.一旦完全注册,其中至少一项不能更改.因此,在会话数据中只保留这些是非常安全的.
- 减少用于存储会话数据的空间.
- 无需担心失效/更新陈旧数据,用户可以立即看到数据库中的任何更改(例如,在下一页请求中).
- PHP 服务器不再负责跟踪数据,更容易将数据与数据库服务器分离,更容易让多个 PHP 服务器与数据库服务器通信.
- 必须经常查询数据库.