What are the potential risks of attaching the Session ID to the URL in PHP?

Attaching the Session ID to the URL in PHP can expose sensitive information to potential attackers through methods like session fixation or session hijacking. To mitigate this risk, it is recommended to use cookies to store and manage session IDs instead of passing them through URLs.

<?php
// Start session
session_start();

// Set session ID in a secure cookie
session_regenerate_id();
setcookie(session_name(), session_id(), null, '/', null, true, true);
?>