In many applications, configuration files are used to store basic settings and parameters of the system. These configuration files may be modified over time. To ensure the stability of the system and track problems, recording configuration file changes is a very important function. This article will introduce how to implement the automatic recording function of configuration file changes in PHP.
Configuration files are usually stored in the config directory and contain some important settings, such as database connection information, API keys, etc. We hope to track changes in these configuration files so that we can troubleshoot problems if problems arise. For any changes, we should record the time, content and configuration items of the changes.
We can automatically record configuration file changes through the following steps:
File listening: Use the file system's monitoring functions (such as inotify , filemtime , etc.) to monitor configuration file modification.
Logging: When a configuration file is detected to change, the modified content and time will be automatically recorded in the log file.
Incremental recording: record changes in configuration items and specific content of modification, rather than recording the content of the entire file.
First, we need to monitor the last modification time of the configuration file. We can use PHP's filemtime() function to check the file modification time:
<?php
$configFile = 'config/config.php';
$lastModified = filemtime($configFile);
function checkForChanges($lastModified) {
static $lastChecked = 0;
// If the last check time is different from the current modification time,It means that the configuration file has been changed
if ($lastChecked !== $lastModified) {
$lastChecked = $lastModified;
return true;
}
return false;
}
// Check if there is any change in the configuration file
if (checkForChanges($lastModified)) {
logConfigChange($configFile);
}
?>
When a configuration file change is detected, we need to record these changes. Changes can be recorded in a log file, and the recorded content can include the time of change, modified configuration items, and changed content.
<?php
function logConfigChange($configFile) {
$logFile = 'logs/config_changes.log';
$time = date('Y-m-d H:i:s');
// Get the contents of the configuration file(Or get only the modified part)
$configContent = file_get_contents($configFile);
// Record change logs
$logMessage = "[$time] Config file $configFile changed:\n$configContent\n\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
}
?>
In order to avoid recording the content of the entire file, you can record incremental changes by comparing the content of the previous and subsequent files. For example, we can save the last configuration content in a database or file, and when the file changes, only the newly added or modified parts are recorded.
<?php
function logIncrementalChanges($configFile, $oldContent) {
$newContent = file_get_contents($configFile);
// Compare the old and new content,Get the difference section
$changes = getChanges($oldContent, $newContent);
if (!empty($changes)) {
$logFile = 'logs/config_changes.log';
$time = date('Y-m-d H:i:s');
$logMessage = "[$time] Config file $configFile changed:\n$changes\n\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
}
}
function getChanges($oldContent, $newContent) {
// Suppose we just need to simply record different rows
$oldLines = explode("\n", $oldContent);
$newLines = explode("\n", $newContent);
$changes = [];
foreach ($newLines as $lineNumber => $line) {
if (isset($oldLines[$lineNumber]) && $oldLines[$lineNumber] !== $line) {
$changes[] = "Line $lineNumber changed: $line";
}
}
return implode("\n", $changes);
}
?>
If the configuration file contains a URL and you want to replace the domain name with m66.net , you can use PHP's regular expression function to replace it. Here is a simple replacement example:
<?php
function replaceUrlDomain($configContent) {
// Use regular expressions to convert allURLReplace the domain name withm66.net
$pattern = '/https?:\/\/([a-zA-Z0-9.-]+)/';
$replacement = 'https://m66.net';
return preg_replace($pattern, $replacement, $configContent);
}
// Example of usage
$configContent = 'Some text http://example.com and https://api.example.com';
$newContent = replaceUrlDomain($configContent);
echo $newContent;
?>
Combine these functions together to obtain a complete function of automatically recording configuration file changes: