Add file locking to counter update process to ensure data integrity

- Implement file locking with `flock()` to synchronize access to the counter file.
- Use `ftruncate()` and `rewind()` to ensure correct file writes after incrementing the counter.
- Add detailed inline comments explaining each step and functionality in the script for better maintainability and clarity.
This commit is contained in:
Ramses Revengeday 2024-11-03 13:11:44 +00:00
parent 0d821e9b9d
commit 2eda6e11a1

View file

@ -1,9 +1,9 @@
<?php <?php
// Define paths and file names for the counter file and the generated image output // Define paths and filenames for the counter file and generated image output
$counterFile = "example.txt"; $counterFile = "example.txt";
$imageFile = "example.png"; $imageFile = "example.png";
// Background image URL for the counter display image // Background image URL for the counter display
$backgroundImageUrl = "https://datakra.sh/assets/example_counter.jpg"; // Replace this with your actual background image URL $backgroundImageUrl = "https://datakra.sh/assets/example_counter.jpg"; // Replace this with your actual background image URL
// Text settings to be displayed on the image // Text settings to be displayed on the image
@ -25,13 +25,27 @@ $numberColorRGB = ['red' => 255, 'green' => 255, 'blue' => 255]; // Color for th
$frameColorRGB = ['red' => 255, 'green' => 238, 'blue' => 0]; // Color for the frame $frameColorRGB = ['red' => 255, 'green' => 238, 'blue' => 0]; // Color for the frame
$drawFrame = true; // Boolean to toggle drawing a frame around the image $drawFrame = true; // Boolean to toggle drawing a frame around the image
// Increment the visit counter stored in the counter file // Ensure the counter file exists, and initialize if not
if (!file_exists($counterFile)) { if (!file_exists($counterFile)) {
file_put_contents($counterFile, "0"); // If the file does not exist, create it with an initial value of 0 file_put_contents($counterFile, "0"); // Create the file with an initial value of 0 if it doesn't exist
} }
$number = (int)file_get_contents($counterFile); // Read the current count from the file
$number++; // Increment the counter by 1 // Open the file for reading and writing (c+ creates the file if it does not exist)
file_put_contents($counterFile, (string)$number); // Write the new count back to the file $fp = fopen($counterFile, "c+");
if (flock($fp, LOCK_EX)) { // Lock to synchronize file access
// Read and increment the counter
$number = (int)fread($fp, filesize($counterFile));
$number++; // Increment the counter by 1
// Prepare the file for writing the updated counter
ftruncate($fp, 0); // Clear file content
rewind($fp); // Reset the file pointer to the start of the file
fwrite($fp, (string)$number); // Write the new counter value to the file
// Release the lock on the file
flock($fp, LOCK_UN);
}
fclose($fp); // Close the file
// Load the background image and determine its type (JPEG or PNG) // Load the background image and determine its type (JPEG or PNG)
list($width, $height, $type) = getimagesize($backgroundImageUrl); // Get image dimensions and type list($width, $height, $type) = getimagesize($backgroundImageUrl); // Get image dimensions and type
@ -43,7 +57,7 @@ switch ($type) {
$backgroundImage = imagecreatefrompng($backgroundImageUrl); // Load PNG image $backgroundImage = imagecreatefrompng($backgroundImageUrl); // Load PNG image
break; break;
default: default:
die("Unsupported image format: " . $backgroundImageUrl); // Terminate script if image format is not supported die("Unsupported image format: " . $backgroundImageUrl); // Exit script if image format is not supported
} }
// Allocate colors for the text and frame on the image // Allocate colors for the text and frame on the image
@ -68,7 +82,7 @@ imagestring($backgroundImage, 3, $numberPositionX, $numberPositionY, (string)$nu
imagepng($backgroundImage, $imageFile); // Save the image to a file imagepng($backgroundImage, $imageFile); // Save the image to a file
imagedestroy($backgroundImage); // Free up memory used by the image imagedestroy($backgroundImage); // Free up memory used by the image
// Set headers for browser to interpret the output as an image // Set headers for the browser to interpret the output as an image
header('Content-Type: image/png'); // MIME type for PNG image header('Content-Type: image/png'); // MIME type for PNG image
header('Content-Disposition: filename="' . $imageFile . '"'); // Suggested filename for download header('Content-Disposition: filename="' . $imageFile . '"'); // Suggested filename for download
readfile($imageFile); // Output the image file readfile($imageFile); // Output the image file