How does PHP handle sessions when cookies are not allowed by the browser?

When cookies are not allowed by the browser, PHP can use URL rewriting to pass the session ID in the URL. This involves appending the session ID to all links and form submissions on the website. This allows PHP to maintain session data across multiple pages even when cookies are disabled.

<?php
session_start();

if (!isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 1;
} else {
    $_SESSION['counter']++;
}

echo "You have visited this page " . $_SESSION['counter'] . " times.";

session_write_close();
?>