What are the advantages and disadvantages of storing session IDs in cookies versus passing them in URLs in PHP?
Storing session IDs in cookies is generally more secure than passing them in URLs, as cookies are not visible in the browser's address bar and are less susceptible to being intercepted. However, storing session IDs in cookies can also open up the possibility of session hijacking if the cookie is not properly secured. Passing session IDs in URLs can make them more vulnerable to attacks like session fixation, where an attacker can force a user to use a specific session ID.
// Storing session IDs in cookies
session_start();
// Set session ID in a secure cookie
$session_id = session_id();
setcookie('session_id', $session_id, time() + 3600, '/', '', true, true);
// Access session ID from cookie
$session_id = $_COOKIE['session_id'];
```
```php
// Passing session IDs in URLs
session_start();
// Set session ID in URL parameter
$session_id = session_id();
$url = "http://example.com/page.php?session_id=$session_id";
// Access session ID from URL parameter
$session_id = $_GET['session_id'];