From 2eda6e11a14e1d87843f9f2bcc501e88273be6d5 Mon Sep 17 00:00:00 2001 From: revengeday Date: Sun, 3 Nov 2024 13:11:44 +0000 Subject: [PATCH] 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. --- script.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/script.php b/script.php index 407fb27..ca7368a 100644 --- a/script.php +++ b/script.php @@ -1,9 +1,9 @@ 255, 'green' => 255, 'blue' => 255]; // Color for th $frameColorRGB = ['red' => 255, 'green' => 238, 'blue' => 0]; // Color for the frame $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)) { - 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 -file_put_contents($counterFile, (string)$number); // Write the new count back to the file + +// Open the file for reading and writing (c+ creates the file if it does not exist) +$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) list($width, $height, $type) = getimagesize($backgroundImageUrl); // Get image dimensions and type @@ -43,7 +57,7 @@ switch ($type) { $backgroundImage = imagecreatefrompng($backgroundImageUrl); // Load PNG image break; 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 @@ -68,7 +82,7 @@ imagestring($backgroundImage, 3, $numberPositionX, $numberPositionY, (string)$nu imagepng($backgroundImage, $imageFile); // Save the image to a file 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-Disposition: filename="' . $imageFile . '"'); // Suggested filename for download readfile($imageFile); // Output the image file