How can one ensure that the counter increments correctly after a page refresh in PHP?

When a page is refreshed in PHP, the state of variables is reset, which can cause issues with incrementing a counter. To ensure that the counter increments correctly after a page refresh, you can use sessions to store the counter value and update it accordingly.

<?php
session_start();
if (!isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 0;
}

$_SESSION['counter']++;

echo "Counter: " . $_SESSION['counter'];
?>