Is it necessary to use session cookies instead of displaying session IDs in URLs for security reasons?

Using session cookies instead of displaying session IDs in URLs is recommended for security reasons. Session IDs in URLs can be easily exposed and intercepted, leading to session hijacking attacks. Session cookies are stored on the client side and are not visible in the URL, making them a more secure option for managing sessions.

<?php
// Start a session
session_start();

// Set session cookie parameters for security
session_set_cookie_params([
    'httponly' => true,
    'samesite' => 'Strict'
]);

// Use session variables as needed
$_SESSION['username'] = 'example_user';
?>