简体   繁体   English

存储主题“视图”的最佳方法

[英]Best way to store “views” of an topic

I use this code to update views of an topic. 我使用此代码来更新主题的视图。

UPDATE topics 
SET views = views + 1 
WHERE id = $id

Problem is that users likes spam to F5 to get ridiculous amounts of views. 问题是用户喜欢向F5发送垃圾邮件以获取可笑的数量。

How should I do to get unique hits? 如何获得独特的点击? Make a new table where I store the IP? 在我存储IP的地方创建一个新表? Don't want to store it in cookies. 不想将其存储在cookie中。 It's too easy to clear your cookies. 清除cookie太容易了。

I would create a separate table for storing this information. 我将创建一个单独的表来存储此信息。 You can then capture a larger amount of data and not require updating the table that is likely to be read the most. 然后,您可以捕获大量数据,而无需更新可能最常读取的表。

You would always use INSERT INTO tblTopicViews... 您将始终使用INSERT INTO tblTopicViews ...

And you would want to capture as much information as you can, IP address, date and time of the hit, perhaps some information on browser version, operating system etc - whatever you can get your hands on. 而且,您可能希望捕获尽可能多的信息,IP地址,命中日期和时间,或者可能包含有关浏览器版本,操作系统等的一些信息-可以动手使用。 That way, you can fine-tune how you filter out refresh requests over time. 这样,您就可以调整随着时间的推移过滤出刷新请求的方式。

It's worth bearing in mind that many users can share an IP - for example, an entire office might go via the same router. 值得牢记的是,许多用户可以共享IP-例如,整个办公室可能会通过同一路由器。

I would create a table which stores unique views: 我将创建一个存储唯一视图的表:

CREATE TABLE unique_views(
    page_id number,
    user_agent varchar2(500),
    ip_address varchar2(16),
    access_time date,
    PRIMARY KEY (page_id, user_agent, ip_address, access_time)
)

Now if someone accesses the page and you want to allow one view per user per day, you could do 现在,如果有人访问该页面,并且您希望每天允许每位用户一个视图,则可以

INSERT INTO unique_views (:page_id, :user_agent, :ip_address, trunc(SYSDATE, 'day'))

which won't allow duplicate views for the same user during one day. 一天之内不允许同一用户重复查看。 You could then count the views for each page with a simple GROUP BY (example for today's views): 然后,您可以使用一个简单的GROUP BY来计算每个页面的浏览量(今天的浏览量示例):

SELECT page_id, count(*) page_views
FROM unique_views
WHERE access_time = trunc(SYSDATE, 'day')
GROUP BY page_id

Well, you could write the individual page hits to a log table, including identifying information like cookings or IP address. 好吧,您可以将单个页面的点击量写入日志表,包括识别信息,例如烹饪或IP地址。 You can analyze that table at leisure. 您可以随意分析该表。

But the web server probably has a facility for this. 但是Web服务器可能为此提供了便利。 I know both IIS and Apache can create detailed usage logs. 我知道IIS和Apache都可以创建详细的使用日志。 And for both, there's a variety of graphing and analysis tools that keeps things like IP addresses into account. 对于这两者,都有多种绘图和分析工具可将IP地址等因素考虑在内。

So instead of rolling your own logging, you could use the web server one. 因此,您不必使用自己的日志记录,而可以使用Web服务器。

You could use the session_id() to discriminate between different users, obiously you need a separate table to track each visit. 您可以使用session_id()区分不同的用户,很显然,您需要一个单独的表来跟踪每次访问。

UPDATE: I just noticed you don't want to depend on cookies, so this may not be suitable for you. 更新:我只是注意到您不想依赖cookie,因此这可能不适合您。

Note that due to various problems (eg. unknown behavior of cache servers) this kind of thing is always going to be inaccurate and a balance between various factors. 请注意,由于各种问题(例如,缓存服务器的未知行为),这种事情总是不准确的,并且各种因素之间必须保持平衡。 However, for a rough vaguely-secure counter, using a separate table as Karl Bartel and others suggest is a decent solution. 但是,对于一个粗糙而模糊不清的柜台,使用Karl Bartel和其他人建议的单独桌子是一个不错的解决方案。

However, depending on how seriously you take this problem, you may want to leave out "user_agent" - it's far to easy to fake, so if I really wanted to inflate my hit counter I could rack up the hits with a script that called my page with user-agent="bot1", then again from the same IP with "bot2", etc. 但是,根据您对这个问题的重视程度,您可能要忽略“ user_agent”-伪造起来很容易,因此,如果我真的想给我的点击计数器充实,我可以使用一个名为带有user-agent =“ bot1”的页面,然后再次来自具有“ bot2”的相同IP,依此类推。

But then, 2 users behind one IP will be only counted as 1 hit so you lose accuracy - see what I mean about a balance between various factors? 但是,如果一个IP后面有2个用户,则只会算为1次点击,因此您会失去准确性-明白我对各种因素之间的平衡的理解吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM