What are the best practices for setting cookies in PHP to ensure they last for a specific duration?
When setting cookies in PHP, it's important to ensure they last for a specific duration by setting the expiration time correctly. To do this, you can use the `time()` function to get the current timestamp and add the desired duration in seconds to calculate the expiration time. Then, you can set the cookie using the `setcookie()` function with the calculated expiration time.
// Set cookie to last for 1 hour
$expiration_time = time() + 3600; // current time + 1 hour
setcookie('cookie_name', 'cookie_value', $expiration_time);
Keywords
Related Questions
- What best practices should be followed when displaying images in a PHP script?
- What best practices should be followed when working with timestamps and graph visualization in PHP using jpgraph?
- In the provided code snippets, what are the best practices for ensuring proper communication between classes and objects to avoid errors like "Trying to get property of non-object"?