Home > Article > Backend Development > Second-hand recycling website uses user login log function developed in PHP
The second-hand recycling website uses the user login log function developed by PHP
As a second-hand recycling website, the user login log is one of the very important functions. By recording users' login information, website administrators can better manage user accounts and ensure the security of user accounts. In this article, we will introduce how to use PHP to develop user login log function, and attach the corresponding code examples.
First, we need to create a database to store user login information. This can be achieved using MySQL or other relational databases. The following is an example database table structure:
CREATE TABLE `logins` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `ip_address` varchar(45) NOT NULL, `login_time` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
In this table, the id
field is an auto-incremented primary key, user_id
is the user's ID, ip_address
is the IP address when the user logs in, login_time
is the time when the user logs in.
Next, we need to record the login information when the user logs in. In the verification logic of user login, you can add the following code:
// 获取用户的ID和IP地址 $user_id = $_SESSION['user_id']; $ip_address = $_SERVER['REMOTE_ADDR']; // 将登录信息插入到数据库 $query = "INSERT INTO logins (user_id, ip_address, login_time) VALUES ($user_id, '$ip_address', NOW())"; $result = mysqli_query($connection, $query); if ($result) { // 登录信息插入成功 } else { // 登录信息插入失败 }
In the above code, we first obtain the user's ID and IP address, and save them in the variables $user_id
and ## respectively. #$ip_address in. Then, use the
INSERT INTO statement to insert this information into the
logins table. If the insertion is successful, the success situation can be handled in subsequent logic; if the insertion fails, the corresponding processing logic can be added as needed.
// 获取用户的ID $user_id = $_SESSION['user_id']; // 查询用户的登录历史记录 $query = "SELECT ip_address, login_time FROM logins WHERE user_id = $user_id"; $result = mysqli_query($connection, $query); if (mysqli_num_rows($result) > 0) { // 循环输出登录历史记录 while ($row = mysqli_fetch_assoc($result)) { echo "IP地址:" . $row['ip_address']; echo "登录时间:" . $row['login_time']; echo "<br>"; } } else { echo "暂无登录记录"; }In the above code, we first obtain the user's ID, and then obtain the user's login history by querying the database. If there is a record, loop through and output the IP address and login time of each record; if there is no record, output the corresponding prompt information. Through the above development, we successfully implemented the user login log function of the second-hand recycling website. By recording user login information and displaying it to the administrator, account security can be improved and abnormal login behavior can be discovered in a timely manner. Of course, the above code example is just a simple demonstration and can be expanded and optimized according to the specific needs of the project in actual applications.
The above is the detailed content of Second-hand recycling website uses user login log function developed in PHP. For more information, please follow other related articles on the PHP Chinese website!