Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow an action once per session

I have a site that tracks video views... every time the controller is called. One could flood their own video views with some heavy F5ing.

How to make it so a view counts or a method runs only once per session?

def show
  new_views = @video.views + 1
  @video.update_attributes(:views => new_views)
end
like image 682
Kyle Macey Avatar asked May 22 '26 08:05

Kyle Macey


1 Answers

You can create a session variable, probably upon login, like :

session[:is_logged] = 1

Then, every time you are about to increment the counter, just check this variable.

like image 193
Spyros Avatar answered May 24 '26 22:05

Spyros