How can you ensure that only one image is returned per HTTP request when displaying images from a folder in PHP?

When displaying images from a folder in PHP, you can ensure that only one image is returned per HTTP request by using a session variable to keep track of the last displayed image. This way, you can cycle through the images in the folder and display only one image per request.

<?php
session_start();

$images = glob('path/to/images/*');
$lastImageIndex = isset($_SESSION['lastImageIndex']) ? $_SESSION['lastImageIndex'] : 0;

if ($lastImageIndex >= count($images)) {
    $lastImageIndex = 0; // Reset to the first image if we've reached the end
}

$image = $images[$lastImageIndex];
$_SESSION['lastImageIndex'] = $lastImageIndex + 1;

echo '<img src="' . $image . '" alt="Image">';
?>