简体   繁体   English

使用 PHP 对多个页面的唯一访问者点击计数

[英]Unique visitor hit count with PHP for multiple pages

I have a "blog" type website and I want to know how many visits each post gets (unique visitors).我有一个“博客”类型的网站,我想知道每个帖子的访问量(唯一身份访问者)。

I want to store this "visit" count in the "posts" table as a single integer value.我想将此“访问”计数作为单个整数值存储在“帖子”表中。

What's the best way to do this?做到这一点的最佳方法是什么? Do I need to create another table for storing IPs and checking for duplicates or is there a simpler way to do this?我是否需要创建另一个表来存储 IP 并检查重复项,还是有更简单的方法来做到这一点?

I would use Google Analytics API, but to answer the question you asked.我会使用 Google Analytics API,但要回答您提出的问题。

First create a table for "visits".首先为“访问”创建一个表。 You can do it in your existing table but creating a new one would be ideal.您可以在现有表中执行此操作,但最好创建一个新表。 For your rows have "blogID", "IP", "hits".对于您的行有“blogID”、“IP”、“hits”。 You can name your rows accordingly.您可以相应地命名您的行。

I would create a function in PHP for trackViews() and pass the IP address and the current blog post identifier.我将在 PHP 中为 trackViews() 创建一个函数并传递 IP 地址和当前的博客文章标识符。

Example:例子:

function trackViews($IPAddr, $id) {...}

Inside that function first, check to see if the IP has already been stored under that blog post首先在该函数中,检查该 IP 是否已存储在该博文下

SELECT * FROM `visits` WHERE `IP` = $IPAddr & `blodgID` = $id

If the database returns no results, then add the IP to the database which will track unique views.如果数据库未返回任何结果,则将 IP 添加到将跟踪唯一视图的数据库中。

Now if it returns data, update the database hits row to increment +1 to track hits from the same IP address.现在,如果它返回数据,则更新数据库命中行以增加 +1 以跟踪来自同一 IP 地址的命中。 The reason is not only to track all views but public wifi may also get multiple hits which would all be from the same IP address.原因不仅是要跟踪所有视图,而且公共 wifi 也可能会获得多个点击,这些点击都来自同一个 IP 地址。

You can still display data easily either in a cross join or just two separate SQL queries.您仍然可以在交叉联接或两个单独的 SQL 查询中轻松显示数据。

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

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