Current Location: Home> Latest Articles> Complete Guide to Implementing User Login Logs in a CMS System Using PHP

Complete Guide to Implementing User Login Logs in a CMS System Using PHP

M66 2025-07-17

Creating the User Login Log Database Table

To implement the user login log functionality, the first step is to create a corresponding table in the database to store the login information. Using MySQL as an example, create a table named login_logs with fields for user ID, login IP, and login time:

CREATE TABLE login_logs (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    ip_address VARCHAR(45),
    login_time DATETIME
);

The table design is simple and effective for storing key information about each user login.

Implementing the Login Log Recording Function

After a user successfully logs in, it is necessary to record the login information immediately. The following PHP function logLogin demonstrates how to implement this:

// Record user login log
function logLogin($userId, $ipAddress) {
    // Connect to the database
    $conn = new mysqli("localhost", "username", "password", "database");

    // Get current time
    $loginTime = date('Y-m-d H:i:s', time());

    // Construct SQL statement
    $sql = "INSERT INTO login_logs (user_id, ip_address, login_time)
            VALUES ('$userId', '$ipAddress', '$loginTime')";

    // Execute SQL query
    $conn->query($sql);

    // Close database connection
    $conn->close();
}

// Example usage
$userId = 1;
$ipAddress = $_SERVER["REMOTE_ADDR"]; // Get current user's IP address
logLogin($userId, $ipAddress);

This function establishes a database connection, retrieves the current system time, and inserts the user ID, IP address, and login time into the login logs table to save the login record.

Querying User Login Logs

To help administrators review user login activities, the following getLoginLogs function provides the capability to query login logs for a specified user:

// Query login logs
function getLoginLogs($userId) {
    // Connect to the database
    $conn = new mysqli("localhost", "username", "password", "database");

    // Construct SQL statement
    $sql = "SELECT * FROM login_logs WHERE user_id = '$userId'";

    // Execute SQL query
    $result = $conn->query($sql);

    // Process query results
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            // Output log information
            echo "ID: " . $row["id"] . "<br>";
            echo "User ID: " . $row["user_id"] . "<br>";
            echo "IP Address: " . $row["ip_address"] . "<br>";
            echo "Login Time: " . $row["login_time"] . "<br><br>";
        }
    } else {
        echo "No login logs found.";
    }

    // Close database connection
    $conn->close();
}

// Example usage
$userId = 1;
getLoginLogs($userId);

This function queries the database for all login records of the specified user and outputs each record line by line for easy viewing and analysis by administrators.

Summary

By following the above steps, a basic user login logging feature has been implemented, including both storing and querying logs. This functionality not only enhances website security management but also provides data support for further user behavior analysis. Developers can extend this feature as needed, such as adding log filtering, exporting, or statistical analysis.