如何追踪帖子浏览量? [英] How to track post views?

查看:144
本文介绍了如何追踪帖子浏览量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在我的项目中添加一个功能,它可以告诉我们这个帖子被查看了多少次,但却无法知道它是如何完成的。我不是一个编程专家,但仍然对php,mysql有很好的了解,所以如果有人能告诉我它是如何完成的。目前我在主页面上显示所有帖子,并且点击帖子用户的标题被重定向到帖子页面显示完整帖子所以肯定会在帖子页面上做一些事情,但我必须计算他们的数量?

I was looking forward to add a feature to my project which can tell us how many times the post has been viewed, but just can't get how is it done. I am not a programming expert but still have good knowledge of php, mysql so if anybody can just give me idea how it is done. Currently I display all posts on main page and on clicking title of post user gets redirected to post page were full post is displayed so surely something is to be done on post page but what I have to count their?

推荐答案

首先要理解的是:HTTP是无状态。如果不能记住HTTP请求和响应之间的任何内容。因此,您需要将外部数据存储中的HTTP请求数量保留到某个页面,该外部数据存储可以是数据库,甚至是服务器主机文件系统的普通文件。



现在,帖子被查看了多少次可能是未定义的。如果帖子是单独的URL,您可以使用此URL计算HTTP请求。据我了解,你提到了一个单独的帖子页面。这意味着您可以直接在帖子页面上计算请求数。当然,PHP代码应该自动添加到所有这样的页面。但这是否意味着你为每个帖子添加一个文件?它可能效率低下。相反,您可以将所有帖子存储在数据库中并动态生成页面,这是PHP和其他服务器端技术的主要任务。



如果你想要计算一些点击(可能需要,特别是,如果帖子存储在同一页面上,所以你可能需要在用户扩展某个树节点或类似的东西时检测事件),你需要处理JavaScript中相应HTML元素的相应事件(比如点击)并通过Ajax通知服务器端:

http://en.wikipedia.org/wiki/Ajax_(编程) [ ^ ],

http:// api.jquery.com/jquery.ajax [ ^ ]。



顺便说一句,我记得有些公司es提供了一些外部Web服务,旨在计算其客户的某些网页的访问量,收费或免费。我个人怀疑它们的价值,但有些网站使用它们。如果您有兴趣,可以自己搜索。



-SA
First thing to understand: HTTP is stateless. If cannot "remember" anything between HTTP requests and responses. Therefore, you need to persist the number of, say, HTTP requests to a certain page, in an external data storage, which can be database or even plain file of the file system of the server's host.

Now, "how many times the post has been viewed" can be something undefined. If the post was a separate URL, you could count HTTP requests with this URL. As far as I understand, you mentioned a separate "post page". It means that you can count the number of requests directly on the post's page. Naturally, the PHP code should be automatically added to all such pages. But does it mean that you add a file for each post? It might be inefficient. Rather, you could store all posts in a database and generate the page on the fly, which is the main task for PHP and other server-side technologies.

If you want to count some clicks instead (which might be required, in particular, if the posts are stored on the same page, so you may need to detect an event when a user expands some tree node, or something like that), you would need to handle appropriate event (say, "click") of corresponding HTML element in JavaScript and inform the server side through Ajax:
http://en.wikipedia.org/wiki/Ajax_(programming)[^],
http://api.jquery.com/jquery.ajax[^].

By the way, I remember that some companies offered some external Web services designed to count visits of some Web pages of their clients, for a fee or free of charge. I personally doubt their value, but some sites used them. If you are interested, you can do your own search.

—SA


这篇关于如何追踪帖子浏览量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆