如何计算有多少人阅读文章

I am programming a Blog. I am interested how to add a counter to the side to see how many people read the different articles.

I understood that if you have a Website counter, than you would normally save the IP and the Date in the database so you only count the person once.

But how would I make sure that a person reading a specific article is only counted once for that article?

The database would turn very big if I would save each Visitor IP for each Article. It came now into my mind to maybe create a Session for every Visitor. Within this Session I save each ArticleID the Person read. So before I update the counter in the database, I check if it already exists in Session - if not increment counter and save in Session otherwise dont.

Is this the right approach or is there another way to do it?

I would set a cookie on the user's browser and send it to the server. If the cookie is new, it's a new user reading the article for the first time.

It depends on which data you want to save. You can save:

  1. Number of hits (count all people/bots/... who opened the pages), this makes fake impression of readers, because doing a refresh will count me twice.

  2. Number of unique visitors, create a session for the user. and increment the counter only once in the session. This can be improved by setting permanent cookie, but looks like overkill for me. and by the way, I can open the article and don't read it!

  3. Number of people interact with the article. You can put some javascript capture (mouse scrolling, clicks on article text, ...) , split text into parts with "read more" ... etc.

If I were you, I will name it "number of views", and use solution 2. And combine this with google analytics (or similar solution) to know more details about visitors.

I prefer to use Google Analitycs API for such tasks. It' hard to implement for the first time - but once made it will help you on any project.

You can read here - http://code.google.com/intl/en/apis/analytics/docs/gdata/home.html

Google analytics is better way

But if you want to do it by programming then follow the steps.

i) In your table from the data of particular article comes just add one column no_of_view or whatever you want to give..

ii) In the starting of page write down the code of php for ::

     from the url you can get the id of current article.
     then from select query get the value of column no_of_view for particular ID (article)
     then fire a query for update that record for no_of_view column with +1

   If you want to get that more accurate then add ipaddress column and date also....

I think, you can execute an ajax function, when user click on article to update column in database table field name 'number_of_click' as per article id and other relevant reference

and show it by simple query 'SELECT no_of_click FROM article WHERE id='{$individual_article_id} ....'';