How does PHP handle sessions and user identification when cookies are not allowed?

When cookies are not allowed, PHP can still handle sessions and user identification by using URL parameters to pass the session ID between pages. This involves appending the session ID to the URL of each page and then retrieving it on subsequent pages to maintain session state.

<?php
session_start();

// Generate a unique session ID
$session_id = session_id();

// Append the session ID to URLs
echo '<a href="page2.php?sid=' . $session_id . '">Go to Page 2</a>';
?>

// On page2.php
<?php
session_start();

// Retrieve session ID from URL
if(isset($_GET['sid'])) {
    session_id($_GET['sid']);
}

// Use session variables as usual
echo 'Welcome back, ' . $_SESSION['username'];
?>