How does setting a cookie in PHP work and what are potential issues that can arise with cookie handling?
Setting a cookie in PHP involves using the setcookie() function with appropriate parameters such as name, value, expiration time, path, domain, etc. Potential issues that can arise with cookie handling include setting cookies before any output is sent to the browser, not properly sanitizing cookie values, and not setting secure and HttpOnly flags for sensitive cookies.
// Set a cookie with secure and HttpOnly flags
setcookie('user_id', '123', time() + 3600, '/', '', true, true);