How can cookies affect the values of SESSION variables in PHP?

Cookies can affect the values of SESSION variables in PHP if they are not properly managed. If a cookie with the same name as a SESSION variable is set, it can overwrite the SESSION variable's value. To prevent this, you should always make sure to use session_start() before setting or accessing SESSION variables to ensure they are not inadvertently overwritten by cookies.

<?php
session_start();

// Set SESSION variable
$_SESSION['user_id'] = 123;

// Set cookie with the same name
setcookie('user_id', 456, time() + 3600);

// Access SESSION variable
echo $_SESSION['user_id']; // Output: 123
?>