Best technique to track web page hit count and catch some abuse
I have an ASP.NET MVC web site with a database of songs. I'd like to track how many page views each song is getting, but I don't want to double count if people are hitting it multiple times a day. If a user goes to a song 10 times a day, I only want my count to get incremented once a day.
At first, I was thinking of using Sessions or something else on the server to track it but this won't work well if I deploy to a farm (since Session will be per server and the user might hit different servers). So I was thinking of using cookies, but I want to know if there are other suggestions? (and if I'm to use cookies, what's the be开发者_Python百科st way to store the info)
If these are anonymous users, cookies are your best bet. You would have to make sure they're not session cookies, which would expire once the user closes the browser.
One option could be ASP.NET profiles, but there are ugly data storage issues unless you want to roll your own persistance there.
Cookie is generally the right way to go, though the trick there is to make sure you aren't counting bots.
Another angle would be to steal a page from Google's book and put the tracking as asynchronous javascript. Then you'd need to cookie visitors and push the ID out to the client side (or read it from JS if you like such things). But that would generally eliminate bots as most of them don't do javascript. Yet.
Write a http module, store the IP and the song in a database.
When you do the reporting, use
select DISTINCT IP, SONG_NAME FROM T_SongAccess
Alternately,
select count(*) from T_SongAccess
WHERE SONG_NAME = 'XY'
AND IP='abc'
and date_access between today:00:00:00 and today:23:59:49
if count > 0 then
-- don't register, already accessed today
else
-- register access
end if
精彩评论