❮ PHP Network Reference Show ExampleThe following example creates a cookie named "user" with the value "John Doe". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer). We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set: <!DOCTYPE html> <?php </body> Try it Yourself » Definition and UsageThe setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. The name of the cookie is automatically assigned to a variable of the same name. For example, if a cookie was sent with the name "user", a variable is automatically created called $user, containing the cookie value. Note: The setcookie() function must appear BEFORE the <html> tag. Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead). Syntaxsetcookie(name, value, expire, path, domain, secure, httponly); Parameter Values
Technical Details
More ExamplesExampleSeveral expire dates for cookies: <?php // cookie will expire when the browser close // cookie will expire in 1 hour // cookie will expire in 1 hour, and will only be available ...some code... </body> Try it Yourself » ExampleTo modify a cookie, just set (again) the cookie using the setcookie() function: <?php <?php </body> Try it Yourself » ExampleTo delete a cookie, use the setcookie() function with an expiration date in the past: <?php <?php </body> Try it Yourself » ExampleCreate a small script that checks whether cookies are enabled. First, try to create a test cookie with the setcookie() function, then count the $_COOKIE array variable: <?php <?php </body> Try it Yourself » ❮ PHP Network Reference How do I set cookies to expire time?Extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the 'expires' attribute to a date and time.
How do I make cookies in 5 minutes?“javascript cookie expire in 5 minutes” Code Answer. function createCookie(name,value,minutes) {. if (minutes) {. var date = new Date();. date. setTime(date. getTime()+(minutes*60*1000));. var expires = "; expires="+date. toGMTString();. } else {. var expires = "";. Is cookie Max Age in seconds?Sets the maximum age of the cookie in seconds. This will result in a cookie1 to expire in 20 seconds.
How long do Javascript cookies last?Persistent cookies are not deleted by the browser when the user closes it. These cookies have an expiration date that you can set in your server. You can set a cookie to expire in a day or ten years.
|