# Page views counter

**URL:** <https://www.ruby-forum.com/t/page-views-counter/154999>\
**Category:** Rails\
**Created:** [December 6, 2008, 12:09am UTC](https://www.ruby-forum.com/t/page-views-counter/154999 "2008-12-06T00:09:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [December 6, 2008, 12:09am UTC](https://www.ruby-forum.com/t/page-views-counter/154999/1 "2008-12-06T00:09:03Z")

</div>

Hi all,

I’ve made a really simple page views counter.

def show  
@post = Post.find…  
addcount = @post.views += 1  
@post.update\_attribute “views”, addcount  
end

Its working fine, but if someone hold the F5 key, it keeps counting and  
counting. Which is not a really good. Is there a way how to force the  
counter to not keep counting while someone is holding the F5 key?

THX P.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [December 6, 2008, 12:24am UTC](https://www.ruby-forum.com/t/page-views-counter/154999/2 "2008-12-06T00:24:18Z")

</div>

On Fri, Dec 5, 2008 at 5:09 PM, Petan C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve made a really simple page views counter.
> 
> def show  
> @post = Post.find…  
> addcount = @post.views += 1  
> @post.update\_attribute “views”, addcount

You don’t need the extra variable.

@post.update\_attribute ‘views’, @post.views + 1

> end
> 
> Its working fine, but if someone hold the F5 key, it keeps counting and  
> counting. Which is not a really good. Is there a way how to force the  
> counter to not keep counting while someone is holding the F5 key?

Store their IP address and a timestamp and then don’t count more views  
from that same IP for a while.

For the IP address use request.env[‘HTTP\_X\_FORWARDED\_FOR’] if your  
Rails app is behind a proxy, or request.env[‘REMOTE\_ADDR’] if it’s  
not.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
