# PHP Cookie expiration



## Firestone (Dec 24, 2006)

Can I set a PHP cookie to expire when the user closes their browser? Also, can I set the cookie to never expire? Like if they choose always logged on.


----------



## Gibbs (Feb 28, 2005)

You can't directly expire cookies when a browser is closed. You can expire them, however, at the end of a session.

Example:

```
setcookie(var1, var2, 0);
```
You will want to search for more detail on that topic .

I've found from personal experience that cookies that have long expiration dates can be problematic, especially with login systems.

To answer your question no. Cookies can't be infinite. However you can set the expiration time. An example below lasts for 10 years:

```
setcookie( var1,var2 ,time()+60*60*24*365*10);
```


----------



## MMJ (Oct 15, 2006)

I am pretty sure you can remove the cookies once the browser is closed however.


----------

