Re: Counting unique viewers?

Greg Donald
2007-02-06T22:47:19Z

On 2/6/07, Jeff Coleman
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:
> I''m building an app where I''d like to track the number of
views of each
> item, but I''d like to keep it from being spammable, so it counts
unique
> viewers rather than just the number of pageviews.
>
> What''s a good way to go about this?  Should I build a database to
record
> the IP addresses and save the request.remote_addr for each viewer in the
> database, associated with each item?  And then check each viewer against
> that item''s database of IPs to see if they''re unique
before incrementing
> the view counter?
>
> Are there other ways of doing it?

You have to take into consideration the ISPs who force user requests
through their proxy servers.  Those users will appear to have the same
IP address a lot of the time, AOL users for example.  I usually go
with checking that the UP address isn''t new in the last 24 hours or
so, since dial up users stand a good chance of getting assigned a
different proxy their next session.

If you''re using MySQL I wouldn''t store raw IP addresses.  That
will
cost you a varchar(15) field which may be up to 16 bytes.  Instead
convert the IP to a signed integer before storing it.  That will only
cost you 4 bytes, until ipv6 gets more love anyway.

def ip2long( ip )
  long = 0
  ip.split( /\./ ).reverse.each_with_index do |x, i|
    long += x.to_i << ( i * 8 )
  end
  long
end


--
Greg Donald