Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick question: How to set a cookie that will expire after 90days in PHP?

Tags:

php

cookies

I wish to set a cookie that expires in 90 days using PHP, how could I do that? Thanks in advance.

like image 684
Ahmad Fouad Avatar asked Nov 09 '10 21:11

Ahmad Fouad


People also ask

How do I set cookies to expire time?

You can 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 you set cookies to expire in 30 days?

Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes). The path on the server in which the cookie will be available on.

How long does a cookie last PHP?

PHP Create/Retrieve a Cookie 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).

What happens when a cookie expires PHP?

name: The name of the cookie. value: The value you want to store in the cookie. expire-time: It is the number of seconds until the cookie will be kept on the user's machine by the browser. After that, it will automatically be deleted.


2 Answers

setcookie(name, value, time()+60*60*24*90); 

This will set the cookie for 90 Days.

like image 170
Paul Sonier Avatar answered Oct 20 '22 21:10

Paul Sonier


cookie expirations are set in seconds: so 60*60*24*90 would be 90 days

setcookie("MyCookie", $value, time()+(60*60*24*90)); 
like image 24
FatherStorm Avatar answered Oct 20 '22 21:10

FatherStorm