Sessions in Rails


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.


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.

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] -                     unless @time_left > 0                     reset_session                     render update do |page|                           page.redirect_to path for login page                     end                end

2) Add below remote periodic call to the layout.

<%= periodically_call_remote :url => { :action => ' session_expiry' }, :update => ' ' %>

Thats Done !!

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.