What are the best practices for passing session IDs in PHP, considering user preferences for cookies?

When passing session IDs in PHP, it is recommended to use cookies as the primary method for storing and retrieving session IDs. This is because cookies are more secure and convenient for users compared to passing session IDs through URLs. By setting the session ID as a cookie, you can ensure that the session remains secure and is not exposed in the URL, which can be vulnerable to attacks.

// Start the session
session_start();

// Set the session ID as a cookie
setcookie(session_name(), session_id(), time() + 3600, '/');

// Retrieve the session ID from the cookie
$session_id = $_COOKIE[session_name()];