简体   繁体   中英

Calculating page views in php / MySQL

I have a table article with many articles. I want to track the number of viewers for each article. Here's my idea, of how I plan to do it.

I've a table viewers with rows: id , ip , date , article_id .

article_id is a FOREIGN FIELD referring to the id of the article. So, when a user open up an article, his/her IP address is stored in the table.

Is this a decent approach? It is for a personal site and not a very big website.

EDIT : I want to print the number of view on each article page.

It depends on how frequently you need to display number of viewer. Your general query will be:

select count(*) from viewers
where article_id='10'

With time, your viewers table will grow. Say it have million records after 1 year or two. Now if you are showing number of viewers on each article page or displaying articles with most viewers, it will start impacting on performance even though foreign key is indexed. However that will happen after you added hundreds of articles with each having thousands of viewers.

A better optimized solution may be to keep number of viewers in article table and use that to display results. Existing Viewers table is also necessary to ensure there is no duplicate entry (Same user reading an article ten times must be marked as single entry not ten).

Use a Tool like Google Analytics. This will do the job much more elaborated and you're up and running in minutes, there's more about unique visitors than IP addresses!

If you want to have an on premise solution, look at PIWIK , which is PHP framework for exactly this puprose.

In this design,There is a one problem if the same user open it again and again then either you have to put check before insert the entry or you insert the same ip address multiple time but different time stamp.

Most of the popular sites consider one ip address as one view even if that client or user open that article several times.

I can think of solution.

  1. your approach with single check. if the same client has opened it again don't insert it. or group by Id when you retrieve the counter.

It depends on what you want to store in your database. If you want to know exactly how many unique users visited this particular article (including date and ip) this is reasonable way do to this. But if you need only a number to show you can alter article table and include new column with visit_counter and store cookie to prevent incrementing counter on same user.

try something like this

// insert
$query = mysqli_query("REPLACE INTO viewers (ip) VALUES ('" . ip2long($_SERVER['REMOTE_ADDR']) . "')");

// retrieve
list($pageviews) = mysqli_fetch_row(mysqli_query("SELECT COUNT(ip) FROM viewers"));

echo $pageviews;

Read : REPLACE INTO

Yes, this is good aproach if you create some kind of cache for displaying how many views each article had. It's not optimal to count views each time user open website.

  1. You can do it in SQL Server. There is something like materialized view . ( https://code.google.com/p/flexviews/ )

     select article_id, count(*) as views from viewers group by article_id 
  2. Or you can cache it in files and refresh every X time.

  3. To store users who viewed article I suggest using AJAX. When user open website, another 'thread' will call website to add his as viewer. Even if your db is slow, it will not slow down website loading, also web spiders will not be counted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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