Home >Backend Development >PHP Problem >What should I do if the PHP browser remains in the login interface before closing?
With the rapid development and popularization of the Internet, more and more websites and applications have begun to provide users with more convenient, efficient, and rich functions and services. In these Internet applications, the user login function has become one of the standard features. Using the login function, users can easily access their personal information, customize their own profiles, enjoy personalized recommendation services, etc. However, in actual use, once the user closes the browser, he still needs to log in again the next time he visits, which will undoubtedly increase the user's inconvenience and annoyance. To this end, PHP provides a solution that allows users to remain logged in before closing the browser. Let's introduce it below.
In a general login system, after a user logs in, the user's login information (such as user name, password, login time, etc.) is usually saved in the SESSION or COOKIE on the server. When the user continues browsing or leaves, the user identity is verified and processed based on the value of SESSION or COOKIE. However, when the user closes the browser, the SESSION or COOKIE will also become invalid. This causes users to need to log in again the next time they visit, which is very inconvenient.
To this end, we can use a solution provided by PHP, which is to save the user's login information in the database. When the user logs in, the user's login information is inserted into a table in the database; when the user logs out, the record is deleted from the table. In this way, after the user closes the browser, the login information will not be deleted. On the next visit, we can retrieve the information from the database and then perform user authentication and processing according to the situation.
In specific implementation, we need to first create a database table to save the user's login information. The table can include the following fields:
When a user logs in, we insert the user’s login information into the table:
//连接数据库 $conn = mysqli_connect("localhost", "user", "password", "demo"); //获取用户输入的用户名和密码 $username = $_POST["username"]; $password = $_POST["password"]; //查询该用户是否已经登录过 $sql = "SELECT id FROM login_info WHERE username='$username'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { //该用户已经登录过了,更新登录时间即可 $login_time = time(); $sql = "UPDATE login_info SET login_time='$login_time' WHERE username='$username'"; mysqli_query($conn, $sql); } else { //该用户是首次登录,将登录信息插入到数据库中 $login_time = time(); $sql = "INSERT INTO login_info (username,password,login_time) VALUES ('$username','$password','$login_time')"; mysqli_query($conn, $sql); }
When When the user logs out, we delete the user's login information from the database:
//获取用户的用户名 $username = $_SESSION["username"]; //将该用户的登录信息从数据库中删除 $sql = "DELETE FROM login_info WHERE username='$username'"; mysqli_query($conn, $sql);
When the user visits the website again, we can take out the user's login information from the database for authentication:
//连接数据库 $conn = mysqli_connect("localhost", "user", "password", "demo"); //从数据库中取出用户的登录信息 $username = $_SESSION["username"]; $sql = "SELECT * FROM login_info WHERE username='$username'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { //该用户已经登录过,验证用户身份 $row = mysqli_fetch_assoc($result); if ($_SESSION["password"] == $row["password"]) { //用户身份验证通过,更新上次访问时间即可 $logout_time = time(); $sql = "UPDATE login_info SET logout_time='$logout_time' WHERE username='$username'"; mysqli_query($conn, $sql); } else { //用户身份验证失败,跳转到登录页面 header("Location:login.php"); } } else { //该用户未登录过,跳转到登录页面 header("Location:login.php"); }
In this way, even if the user closes the browser window, the login information can still be saved in the database, and the user can retain the login status without re-logging in the next time they visit. Of course, we need to make some optimizations to this solution, such as setting automatic logout time, preventing SQL injection, etc., in order to implement this function more safely and reliably.
In short, by saving the user's login information in the database, the user can remain logged in before closing the browser, which greatly improves the user experience and efficiency. It is worth learning and mastering by developers. Technology.
The above is the detailed content of What should I do if the PHP browser remains in the login interface before closing?. For more information, please follow other related articles on the PHP Chinese website!