What potential pitfalls should be avoided when working with cookies in PHP?

One potential pitfall when working with cookies in PHP is not properly sanitizing and validating the data stored in cookies. This can lead to security vulnerabilities such as cross-site scripting attacks. To avoid this, always sanitize and validate any data before storing it in a cookie.

// Sanitize and validate data before storing in a cookie
$data = $_POST['data'];
// Sanitize the data
$sanitized_data = filter_var($data, FILTER_SANITIZE_STRING);
// Validate the data
if (/* validation condition */) {
    setcookie('cookie_name', $sanitized_data, time() + 3600, '/');
}