# Expiring Cookies when the Browser is Closed

**URL:** <https://www.ruby-forum.com/t/expiring-cookies-when-the-browser-is-closed/82680>\
**Category:** Rails\
**Created:** [January 19, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/expiring-cookies-when-the-browser-is-closed/82680 "2007-01-19T18:04:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sfisher](https://www.ruby-forum.com/u/sfisher)\
**Post date:** [January 19, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/expiring-cookies-when-the-browser-is-closed/82680/1 "2007-01-19T18:04:25Z")

</div>

Hi,

The answer to this is probably a one line, but i can’t find it  
anywhere.

How do I make a cookie expire when a browser is closed (ie when the  
session ends)? The expiry key in the cookie hash only seems to take  
dates.

Thanks,  
Scott

---

<div class="post-metadata">

**Author:** ![sfisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sfisher](https://www.ruby-forum.com/u/sfisher)\
**Post date:** [January 19, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/expiring-cookies-when-the-browser-is-closed/82680/2 "2007-01-19T18:24:28Z")

</div>

> The answer to this is probably a one line, but i can’t find it  
> anywhere.
> 
> How do I make a cookie expire when a browser is closed (ie when the  
> session ends)? The expiry key in the cookie hash only seems to take  
> dates.

Set it to a date in the past… like 1.day.ago

---

<div class="post-metadata">

**Author:** ![sfisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sfisher](https://www.ruby-forum.com/u/sfisher)\
**Post date:** [January 19, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/expiring-cookies-when-the-browser-is-closed/82680/3 "2007-01-19T21:57:01Z")

</div>

On Jan 19, 2007, at 12:32 PM, Philip H. wrote:

> > The answer to this is probably a one line, but i can’t find it  
> > anywhere.
> > 
> > How do I make a cookie expire when a browser is closed (ie when the  
> > session ends)? The expiry key in the cookie hash only seems to take  
> > dates.
> 
> Set it to a date in the past… like 1.day.ago

Actually, if you set a cookie without giving an expiration date, it  
is only retained until the browser is closed. (Which can mean the  
program itself, not just a window or a tab.)

An expiration in the past will delete the cookie immediately.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
Skype: rob.biedenharn
