不明白为什么需要使用 $_SESSION。

14 浏览
0 Comments

不明白为什么需要使用 $_SESSION。

我尝试根据一个示例创建一个登录表单。我使它工作了,但我不理解为什么需要$_SESSION['umsco'],以及为什么我要将$username变量赋值给它。我也不明白是否需要conn->close()以及$result变量是什么意思。

//我包含了数据库文件,这样我们就可以从中获取信息并在这里应用。
include "dbc.php";
//这里我们从表'members'中选择我们想要的变量。
$sql = "SELECT id, firstName, lastName FROM members";
//这里我们做了一些我不太清楚的操作,但这是标准程序。
$result = mysqli_query($conn, $sql);
//声明用户名
$username = $_POST["username"];
$password = $_POST["password"];
//我的结果,再次,这是标准程序。
$result = $conn->query($sql);
//我们将所有值插入行,标准程序。
$row = $result->fetch_assoc();
//这里我们检查用户名是否与数据库中的相同。
if ($username == $row["firstName"]) {
    //如果正确,我们开始一个会话
    session_start();
    //我们给会话一些随机的字母或数字,并将其设置为$username,不太确定为什么,但如果没有它,它不会工作。
    $_SESSION['umsco'] = $username; 
    //我们将位置更改为secret.php
    header("location: secret.php");
}
//我们关闭连接,不确定是否需要,但这似乎是合理的。
$conn->close();

0
0 Comments

在这段代码中,$_SESSION用于存储用户的用户名,以便在需要的时候可以随时调用。然而,有些人可能不理解为什么需要$_SESSION,以及它的作用。

原因是,HTTP是一种无状态协议,这意味着服务器在每个请求之间都不会保留任何信息。当用户通过登录表单进行身份验证时,服务器需要一种方法来跟踪用户,并在每个请求之间保留用户的身份信息。这就是$_SESSION的作用。

解决方法是在每个需要使用用户信息的页面上调用session_start()函数,以启动会话。然后,可以使用$_SESSION来存储和检索用户相关的数据,如用户名。这样,在下一次调用session_start()时,就可以通过$_SESSION['umsco']来获取存储的用户名。

总之,会话是一种在无状态的HTTP协议中跟踪用户身份的方法。通过使用$_SESSION,可以在每个请求之间保留用户的身份信息,以便在需要时随时调用。要使用$_SESSION,只需在每个需要使用用户信息的页面上调用session_start(),然后使用$_SESSION存储和检索用户相关的数据。

0
0 Comments

$_SESSION is needed because it allows the storage of data that can be accessed between different requests. In the context of a login form, the $_SESSION variable can be used to remember the user's ID or other relevant information so that the user does not need to submit their username and password with every request.

The reason why $_SESSION is needed in this case is to ensure a seamless user experience. By storing the user's ID or other relevant information in $_SESSION, the application can recognize the user and retrieve their data without requiring them to repeatedly enter their credentials. This improves the efficiency and convenience of the login process.

To implement this functionality, the following steps can be followed:

1. Query the members table using the username and password provided in the $_POST data. This is done to verify the user's credentials and retrieve their relevant information.

2. If the username and password match, store the user's ID or other relevant information in the $_SESSION variable. This can be achieved using the session_start() function followed by assigning the desired value to $_SESSION.

3. In subsequent requests, check if the $_SESSION variable is set and contains the necessary information. If it does, retrieve the user's data using the stored ID or other relevant information. This eliminates the need for the user to re-enter their credentials.

Overall, the use of $_SESSION in this context simplifies the login process and improves the user experience by allowing the storage and retrieval of relevant user information between requests. By implementing this functionality, the application can remember the user and provide a seamless login experience.

0
0 Comments

为了避免不良行为,我建议您始终在代码开头实现session_start()

什么是会话?

为了理解这个问题,您必须先理解什么是PHP会话。

会话是一种在整个网站的所有页面上保持变量的方式,以便为当前用户使用。

它是如何工作的?

首先,您必须要求PHP初始化会话。为此,您必须在代码开头添加session_start()

当服务器响应客户端时,它会附加一个名为PHPSESSID的cookie,其中包含用户的唯一会话标识符。

每次请求时,浏览器都会将此cookie发送到服务器,以便PHP可以从服务器的硬盘中恢复会话。

注册会话变量的最常见方法是$_SESSION['key'] = $value;

最终答案

最后一行$_SESSION['umsco'] = $username;将用户的用户名存储在他的会话中。

- 当会话被session_destroy()销毁时。

- 当会话在php.ini中定义的一段时间后过期时。

- 当您注销变量时。

secret.php中,您可能会检查该值是否被赋值,或者检查会话是否存在,以检查用户是否已登录。这就是为什么它是必需的,否则登录表单将毫无意义。

另一个资源:Is closing the mysql connection important?

0