# Sessions in Rails

**URL:** <https://www.ruby-forum.com/t/sessions-in-rails/142577>\
**Category:** Rails\
**Created:** [July 9, 2008, 11:34am UTC](https://www.ruby-forum.com/t/sessions-in-rails/142577 "2008-07-09T11:34:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piyush\_with\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@Piyush\_with\_Rails](https://www.ruby-forum.com/u/Piyush_with_Rails)\
**Post date:** [July 9, 2008, 11:34am UTC](https://www.ruby-forum.com/t/sessions-in-rails/142577/1 "2008-07-09T11:34:47Z")

</div>

Hi,

I am using sessions in my application and file store for sessions  
storage.

now i want to expire my session after some short of idle time.for that  
i need the start time of the session.

Does Any one know how can i get start time of the session ??

Thanks in Advance.

Piyush.

---

<div class="post-metadata">

**Author:** ![Piyush\_with\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@Piyush\_with\_Rails](https://www.ruby-forum.com/u/Piyush_with_Rails)\
**Post date:** [July 9, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/sessions-in-rails/142577/2 "2008-07-09T12:09:44Z")

</div>

Restful authentication / acts\_as\_authenticated does this by using a  
database field and setting a cookie. Perhaps you could take a look at  
the source code of that.

---

<div class="post-metadata">

**Author:** ![Piyush\_with\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@Piyush\_with\_Rails](https://www.ruby-forum.com/u/Piyush_with_Rails)\
**Post date:** [July 9, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/sessions-in-rails/142577/3 "2008-07-09T16:29:46Z")

</div>

Thanks for your response Ryan… I found one another and easy way for  
doing same without getting start time for sessions.

For doing session timeout after some idle time we can go with below  
thing.

1. Define below filter and methods in application.rb file.

before\_filter :update\_activity\_time, :except =\> :session\_expiry

```
          def update_activity_time
               session[:expires_at] = 10.minutes.from_now
          end

           def session_expiry
               @time_left = (session[:expires_at] - Time.now).to_i
                unless @time_left > 0
                reset_session
                render update do |page|
                      page.redirect_to path for login page
                end
           end

```

1. Add below remote periodic call to the layout.

\<%= periodically\_call\_remote :url =\> {  
:action =\> ’ session\_expiry’ },  
:update =\> ’ ’ %\>

Thats Done !!

---

<div class="post-metadata">

**Author:** ![Piyush\_with\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@Piyush\_with\_Rails](https://www.ruby-forum.com/u/Piyush_with_Rails)\
**Post date:** [July 9, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/sessions-in-rails/142577/4 "2008-07-09T16:57:51Z")

</div>

In case it matters, periodically\_call\_remote _requires_ javascript… so  
if  
a user disables JS, your example doesn’t work unless you do that expiry  
check before every request as well.

On Wed, Jul 9, 2008 at 9:16 AM, Piyush with Rails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
