Hi,

I have articles web project. I want to calculate the views of every article, just like the views of post in forums.

I created a table article_views with article_id, view_date, view_ip so everytime the page will load it will add a record

but that is a problem because anyone can refresh the page to increase the views!

what's the proper way to do it please?

Thanks,
Jassim

    I think a cookie is the best solution. You could check if the ip already exists in your database on that article, but that will not count users behind the same firewall so a cookie is the best i think...

      6 days later

      Personally I would set a session based on the article id. Set it on page load then increment your views column. Not everyone has cookies enabled but either way works!

        but still there is one issue!

        every refresh will increase the views by +1 for the same article and IP address which is not realistic

        How do I control it? just one view per one hour per IP?

        How does Google Analytic and other products overcome this issue?

          Using the session idea from DeadlySin, you would only increment if the session does not contain the current article id. That way only the first view for a person is included.

            if the user closed the browser and opened it again then it's a new session which means one more view! am I right?

              Only if the cookie only lasts for the current session. Nothing says that you are limited in that way. [man]session_set_cookie_params[/man]

                Derokorian;11040931 wrote:

                Only if the cookie only lasts for the current session. Nothing says that you are limited in that way. [man]session_set_cookie_params[/man]

                Except the default settings on most browsers these days?

                  dalecosp;11040955 wrote:

                  Except the default settings on most browsers these days?

                  Er... I'm confused; there generally are no settings in a browser (other than "don't accept cookies at all") that would prevent you from changing the amount of time the session cookie lasts, let alone default values for those settings that would do so.

                    I'm a tad confused too; I've been working on a bug/project that involved setting a cookie 90 days into the future. This cookie was closely identified with sessions, and both Chrome and "The Fox" were expiring the cookie on browser close instead of holding it in storage. I found a setting in Chrome, e voila, the cookie remained. No such luck yet with FFox (but I'm working on Y.A.B. ['YetAnotherBug'] today) ...

                    It could be, of course, that it has more to do with the software that's producing the cookie in the first place than the browser ... still more work to decipher that one, though.

                      Write a Reply...