What are the best practices for handling cookies and session variables in PHP scripts?

When handling cookies and session variables in PHP scripts, it is important to properly set and manage these variables to ensure security and efficiency. Use secure settings for cookies, such as setting the 'secure' and 'httponly' flags to prevent cookie data from being accessed by unauthorized parties. For session variables, ensure that sensitive data is not stored in plain text and consider using encryption or hashing techniques for added security.

// Set secure and httponly flags for cookies
setcookie("cookie_name", "cookie_value", time() + 3600, '/', 'example.com', true, true);

// Store sensitive data in session variables securely
$_SESSION['user_id'] = encrypt($user_id);