What are the best practices for setting the lifetime of a cookie in PHP to ensure it remains valid for a long period?

Setting the lifetime of a cookie in PHP involves specifying the expiration time in seconds from the current time. To ensure a cookie remains valid for a long period, it's recommended to set the expiration time to a distant future date. This can be achieved by adding a large value to the current timestamp.

// Set cookie with a long expiration time (e.g., 1 year)
setcookie("cookie_name", "cookie_value", time() + 31536000, "/");