Track Page Views using PHP

2019-04-10 00:50发布

I'm looking for a way to track the number of visits to a page, without counting duplicates (like someone hitting refresh several times) and I'd like to figure out the best way to do it without keeping track of every single IP address to view the page. Perhaps a cookie? Any other suggestions?

5条回答
ら.Afraid
2楼-- · 2019-04-10 01:29

You mentioned in one of the other comments that you are already using Google Analytics, so you should be able to do the following:

Create a custom report with "Page" as a Dimension and "Unique Visitors" as a metric. This will tell you how many unique Visitors there were for all of your pages for whatever time frame.

查看更多
Ridiculous、
3楼-- · 2019-04-10 01:31

A cookie will fit. Just at each page view you check if it is set. If so, don't count it. Otherwise, count it and set the cookie.

You will have problems with people not having cookies enabled or cleaning them out after each visit. You should pair this approach with PHP sessions: instead of doing the process on each page view, do it only once, on session start. This will at least avoid counting visits within a single session.

All this is you want to store everything client-side. If you were server-side, you could use IP addresses, or pair them with the user agent string.

Or, just use google analytics...

查看更多
再贱就再见
4楼-- · 2019-04-10 01:43

Cookies are a good way to achieve this, however you have to keep in mind that users could have disabled cookies... When a user visits your page, just check if your cookie is already present, if not, count++ :)

查看更多
趁早两清
5楼-- · 2019-04-10 01:44

Either use Webalizer to analyse your logfiles or use Google Analytics to track those metrics for you or - if you are looking for an extendable and self hosted solution, try Piwik:

Piwik is a downloadable, open source (GPL licensed) real time web analytics software program. It provides you with detailed reports on your website visitors: the search engines and keywords they used, the language they speak, your popular pages… and so much more.

In any case, there is no reason to reinvent the wheel here. Also, the software mentioned above is only a tiny fraction of web analysis tools out there.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-04-10 01:47

Store/increment a key/value with redis or memcache, then run a cron job to store values for page/user every x-amount of minutes. Don't need to get complicated with things.

查看更多
登录 后发表回答