node.js - 怎么实现文章浏览次数统计
PHP中文网
PHP中文网 2017-04-17 13:16:31
0
5
881

比如segmentfault首页的浏览次数是怎么统计的。
实现原理是什么?

PHP中文网
PHP中文网

认证0级讲师

reply all(5)
伊谢尔伦

This should depend on the specific needs. If you don’t want to exclude duplicates, just add multiple access statistics fields to the article table. Each time there is a visit, add one. If you want to eliminate duplicates, it will involve login and not logged in. The problem requires a special table to sort out the weights. I have never done it. The general idea is this. I hope it helps

巴扎黑

Simpler: Page refresh-->Field +1
Complicated: separate table to count visits and determine IP repeatability

大家讲道理

Create a variable of type int to save the number of visits, and then save it to the domain of ServletContext, so that all Servlets can access it!
Initially, there are no attributes related to the number of visits saved in the ServletContext;
When this site is visited for the first time, create a variable count and set its value to 1; save it in the ServletContext;
When the site is visited for the first time, When accessed, you can get this variable from the ServletContext, and then add 1 to it.
Get the ServletContext object and check whether there is an attribute named count. If it exists, it means it is not the first time to access. If it does not exist, it means it is the first time to access;
First time access: call setAttribute( of Servletcontext) ) passes an attribute named count with a value of 1;
2nd to Nth visit: call the getAttribute() method of ServletContext to obtain the original number of visits, add 1 to the number of visits, and then call the setAttribute() method of Servletcontext Complete setup.

巴扎黑

Static language has this trouble, so let’s use dynamic language to solve it
http://busuanzi.ibruce.info/

巴扎黑

If it is really realized, you can borrow the existing wheel:

It is reported that Javascript development frameworks include Clouda, Meteor, Firebase...
It can count the number of page visits and update them in real time.
I have only used Firebase. The advantages of Firebase are:
The API is simple, very convenient to use, and can greatly reduce the amount of code.
Data can be managed through the web page, which is very convenient.
Supports SSL, authority authentication, and data security mechanisms. ....

http://www.jeffjade.com/2015/03/14/2015-03-14-hexo-blog/

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template