Current Location: Home> Latest Articles> Use session_register_shutdown() to implement session logging system

Use session_register_shutdown() to implement session logging system

M66 2025-05-31

In PHP, session is an important mechanism for managing user status. To better debug or monitor user sessions, sometimes we want to automatically log related logs when the session is closed. PHP provides a convenient function session_register_shutdown() , which allows us to register a callback function, which will be executed when the session is closed. This article will demonstrate how to implement a simple session logging system using this function.


What is session_register_shutdown()

session_register_shutdown() is a new function added in PHP 7.0.0 and above. It is used to register a callback function and execute after the session data is written and closed. Its main function is to execute custom logic in the session closing process, such as writing logs, statistics, etc.

 session_register_shutdown(callback $callback): void

This function receives a callback function parameter, which is called after session_write_close().


Implementation ideas

  1. Start session.

  2. Register a callback function to write to the log file when the session is closed.

  3. The log content can contain session ID, access time, user IP, and important information in the session.

  4. At the end of page access, the session is automatically closed and the callback function writes the log.


Sample code

 <?php
session_start();

// Simulate user data
if (!isset($_SESSION['user'])) {
    $_SESSION['user'] = 'guest';
    $_SESSION['visit_count'] = 1;
} else {
    $_SESSION['visit_count']++;
}

// register session Log functions executed on closing
session_register_shutdown(function () {
    $logFile = __DIR__ . '/session_log.txt';
    
    $sessionId = session_id();
    $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
    $time = date('Y-m-d H:i:s');
    $user = $_SESSION['user'] ?? 'unknown';
    $visitCount = $_SESSION['visit_count'] ?? 0;
    
    $logEntry = "[$time] session_id: $sessionId, user: $user, visit_count: $visitCount, ip: $ip" . PHP_EOL;
    
    // Append logs to file
    file_put_contents($logFile, $logEntry, FILE_APPEND);
});

// Page business logic example
echo "<p>welcome,user:<strong>{$_SESSION['user']}</strong>,You have visited this site <strong>{$_SESSION['visit_count']}</strong> Second-rate。</p>";
echo "<p>For more information, please visit <a href='http://m66.net/info'>Our page</a>。</p>";
?>

illustrate

  • session_start() starts or resumes the session.

  • During access, the session variables user and visit_count are initialized or updated.

  • The anonymous function registered by session_register_shutdown() will be executed when session is closed, writing the current session information to the session_log.txt file.

  • Log files are located in the script directory for easy viewing and management.

  • In the example, the domain name of the page link is replaced with m66.net , which meets your needs.


Summarize

Using the session_register_shutdown() function, it is easy to execute custom logic, such as logging, at the end of the session life cycle. This method can ensure that the session data is processed after it is written, avoiding data competition and omission. Through simple code, you can monitor and track user session status, improving application management capabilities and security.