What are the best practices for handling sessions in PHP when cookies are disabled?

When cookies are disabled, PHP sessions can be managed by passing the session ID in the URL or using a hidden form field. This allows the server to identify the session without relying on cookies. To implement this, you can check if cookies are enabled and if not, append the session ID to URLs or include it in form submissions.

<?php
session_start();

// Check if cookies are disabled
if (!isset($_COOKIE[session_name()])) {
    // Append session ID to URLs
    ini_set('session.use_trans_sid', true);
}

// Use session ID in form submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $_POST[session_name()] = session_id();
}
?>