Forum Moderators: coopster

Message Too Old, No Replies

Page counter and page refresh

         

spacehere

7:56 am on Mar 4, 2011 (gmt 0)

10+ Year Member



Hi,

I have a very simple question about a page counter.
The code is extremely basic:

$update = 'UPDATE ' . $database . '.table SET totalviews = totalviews + 1 WHERE id = ' . $id;
mysql_query($update);

Further down the page, the number of total views is displayed. Hence, each time you refresh your page, the counter will increase by one.

Everything has worked fine until today... I notice that if I refresh the page the counter increases by one (as expected); however, occassionally it increases by values of 1-5...

So my question: what is actually causing this? It should just increase by one on each page refresh, but why is it sometimes increasing by 2-5 per refresh?

Hope that this made sense!
Cheers!

g1smd

8:01 am on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Presumably, several other people also viewed the page in between the time you refreshed.

Matthew1980

11:29 am on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>Presumably, several other people also viewed the page in between the time you refreshed.

Exactly what I was thinking, Is this a page on a local server, or on your hosting server?

Cheers,
MRb

rocknbil

5:13 pm on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... or search engine spiders . . . do you really want it to increment on a reload/refresh, isn't that going to skew your results?

There are a variety of ways to approach this, but to get at the "why" you need to store some other data in this row, like IP address, browser (HTTP_USER_AGENT), and maybe even a session or unique id, and a referrer. Then you can only update it if that page ID and session id are unique, and only if the referrer is different. I say that because

- visit page direct = user agent, timestamp, with blank referrer
- reload page = referrer still blank, don't increment
- hit a link then another link back to this page, referrer will be the referring page, increment

Many counter systems do it by session, so once a page is viewed it doesn't increment again for that session. Depends on what you want to do with it . . .

spacehere

6:38 pm on Mar 4, 2011 (gmt 0)

10+ Year Member



No one is viewing the page in between the time it's refreshed - I actually tried it on a test page so that no other user would visit.

Example (clicking refresh in the browser):
1 refresh - 1 on the counter
2 refresh - 2 on the counter
3 refresh - 7 on the counter

I'm actually checking the user agent to filter robots (I realize this is not 100% accurate).

"There are a variety of ways to approach this, but to get at the "why" you need to store some other data in this row, like IP address, browser (HTTP_USER_AGENT), and maybe even a session or unique id, and a referrer. Then you can only update it if that page ID and session id are unique, and only if the referrer is different. I say that because..."

In the case of trying this on a test page, I would know the IP, user agent and referrer. I could create a session / unique id though... but I would already know I'm the only person visiting this page.

"Many counter systems do it by session, so once a page is viewed it doesn't increment again for that session. Depends on what you want to do with it . . . "

I actually have another solution on a separate site that is logging IP and session, so I could always apply it to clean up the issue :)

However, I'm left scratching my head as to why this issue is actually occurring... it's almost like refreshing the page reruns the script (or refreshes the page) single or multiple times at random.

I wonder if this could be caused by Google Analytics / advertising / an AJAX voting system that I have on the page... that is the only javascript.

So ultimately, I'm more curious why my page is behaving this why that an actual solution ;)

Thanks very much for everyone's comments so far!

I'm going to run this locally and hopefully get to the bottom of this...