How can PHP be utilized to prevent duplicate database queries when navigating back to a previous page using the browser's back button?

When navigating back to a previous page using the browser's back button, the browser typically reloads the page from the cache, which may result in duplicate database queries being executed. To prevent this, we can use PHP session variables to store the results of the database query and check if the data is already available before querying the database again.

<?php
session_start();

if(!isset($_SESSION['data'])) {
    // Perform the database query and store the results in the session variable
    $data = // Your database query here
    $_SESSION['data'] = $data;
} else {
    // Use the data from the session variable instead of querying the database again
    $data = $_SESSION['data'];
}

// Display the data on the page
foreach($data as $row) {
    // Display each row of data
}
?>