What are some best practices for preventing a counter from being updated on every page refresh in PHP?

To prevent a counter from being updated on every page refresh in PHP, you can use sessions to store a flag indicating whether the counter has already been updated during the current session. By checking this flag before updating the counter, you can ensure that it only increments once per session.

<?php
session_start();

if (!isset($_SESSION['counter_updated'])) {
    // Update the counter here
    $_SESSION['counter_updated'] = true;
}
?>