PHP& MySQL为动态页面计算页面浏览量的最佳方法

问题描述:

为动态网页(例如下面的url示例)计算网页浏览量的最佳方法是什么?我正在使用PHP和MySQL.简短的解释会有所帮助.谢谢!

What is the best way to count page views for dynamic pages like the url example below? I'm using PHP and MySQL. A brief explanation would help. Thanks!

http://www.example.com/posts/post.php? id = 3

通常,表结构如下所示:

Usually the table structure looks like this:

表格页:

id | name            | ... 
==========================
1    Some Page
2    Some Other Page

表pages_views:

table pages_views:

page_id | views
================
1         1234
2         80

pages_views在page_id

where pages_views has a unique index on page_id

用于增加视图的MySQL语句如下所示:

The MySQL statement to increment the views then looks as follows:

INSERT INTO `pages_views` SET views=1 WHERE page_id=?
    ON DUPLICATE KEY UPDATE views=views+1 ;

由于pages_views.page_id是唯一的,因此如果页面的行不存在,则会创建该行;如果存在(即重复键"子句),则计数器将递增.

Since pages_views.page_id is unique, the row for the page will get created if it doesn't exist; if it exists (that's the "duplicate key" clause), the counter will be incremented.

我在这里选择了两个单独的表,因为CMS页通常不经常更新(因此,它们的负载大部分是读取的),而每个视图的页面视图都可以读取和更新.

I chose two separate tables here, as CMS pages usually aren't updated too often (and therefore, their load is mostly reads), whereas page views are read and updated, well, with each page view.