What is the purpose of passing sessions through the URL in PHP?

Passing sessions through the URL in PHP can be useful when cookies are disabled on the user's browser. By passing the session ID through the URL, you can still maintain session state and keep track of user data across multiple pages. However, this method is less secure as session IDs can be easily exposed in the URL and potentially compromised.

<?php
session_start();

// Check if session ID is passed through the URL
if(isset($_GET['PHPSESSID'])) {
    session_id($_GET['PHPSESSID']);
}

// Continue with session handling
// For example, setting session variables
$_SESSION['user_id'] = 123;

// Redirect to another page
header("Location: another_page.php");
exit;
?>