Page 1 of 1

Continuously updated hit counter

Posted: Wed Oct 13, 2010 11:16 am
by coledenkensohn
Please forgive me if the following cannot be done with just PHP (which I am almost positive it cannot), but let me know which scripts this can be done with.

I need to create a hit counter for my website with the following adjustments:

-The hit counter needs to be continuously updated while someone is on the page. So say I get to the page, and the hit counter says 5. While I'm on the page, someone else goes there. It says 6 for them, and now mine (without refreshing) says 6 as well. During my time on the page, it updates to always reflect the number of hits on the page (probably some number pulled from a MySQL database or a simple text file).

-The numbers of the counter should be images, not standard text. These images should have some sort of flip down or sliding effect for the transition.

-The hit counter needs to max out at some number of hits per person per day. So, if I refresh my browser 10 times (or visit 10 pages), all 10 of my hits will affect the counter, but if I refresh 1000 times, only the first 10 will count.

Can someone please point me in the right direction for these things? Ex. let me know which languages I should use and maybe give me some links to examples?

I could probably make a simple hit counter which uses a cookie to only allow 10 hits to count, but I have absolutely no idea how to have it update without refreshing. I could probably figure out how to convert the text numbers into images, but I wouldn't know how to apply a flipping effect when the images change (without css3).

Thanks for any help!

Re: Continuously updated hit counter

Posted: Wed Oct 13, 2010 12:19 pm
by Jonah Bron
PHP would help you with this, but it doesn't do it all. You'll need to learn Javascript, and how to use Ajax/jQuery requests/Mootools requests.

- For continuously updating, you need Ajax. Create a Javascript script that calls a PHP script that retrieves the current count, and then updates.
http://www.w3schools.com/Ajax/ajax_intro.asp


- Why images? Why a flipping animation? You can get cool looking numbers with CSS, and maybe Web Fonts. The flipping animation is purely aesthetic; what kind anyway? Do you mean like upper and lower number halves that flip over?
http://www.w3schools.com/css/default.asp
http://code.google.com/apis/webfonts/do ... uick_Start


- Use sessions. When the user visits, create a session with a number in it. Every subsequent visit, increment that number. Also, every time they visit check the number. If it's under (say) 10, increment the main counter. If not, don't increment the counter. At that point, you can also stop incrementing the number.
http://www.w3schools.com/php/php_sessions.asp

Re: Continuously updated hit counter

Posted: Wed Oct 27, 2010 7:41 pm
by coledenkensohn
Thanks Jonah!

I now have a follow-up question. I learned the basics of AJAX and got a preliminary script up and running. It updates the number of hits every second by doing a MySQL SELECT function. My new question is this:

How many users can I expect to support using this method? Is this going to overwhelm my server (or the database) if there are hundreds of people a day going to the site calling a SELECT function from the MySQL database every second? What about thousands? How about a million people a day on the site for an average of 60 seconds calling the function once every second?

I am just trying to get an idea of how resource-intensive this sort of thing is. Can anyone suggest an alternative method that would be better?

By the way, the working script can be seen here: http://coledenk.heliohost.org/ajax-counter.php

Re: Continuously updated hit counter

Posted: Wed Oct 27, 2010 8:24 pm
by Jonah Bron
8O I would set it to something way less often than one second. If I were you, I'd do it every ten or twenty seconds, and just guess in-between.

For example, say I update and the count is 10. Say I update again and it's 20. If it's at intervals of ten seconds, I know that it's going up one hit per second (ten seconds divided by ten hits reduces to 1sec/hit). Between updates, use that rate to jack up the count by (in this case) one every second. The cool thing is that it's self correcting: every ten (?) seconds it's back on track if it's a bit off.

I'm sure this idea is not new. In fact, I thought of it when I came across another counter like the one you want. It was going way too fast to be updating every interval, so I'm sure it was using this or a similar technique.

Also, are you just storing one row in the database with the count? If you are, it shouldn't be too hard on the server.

Re: Continuously updated hit counter

Posted: Wed Oct 27, 2010 8:49 pm
by coledenkensohn
That is a genius idea! I don't fully understand your explanation of it, but I understand the concept of using the rate to guess. So let me see if I can understand this:

I have a separate row on the table which is "rate" and a script which checks the number of hits every minute (I will say minute to be completely safe). It creates a rate by calculating (current number of hits/last known number of hits). Then, the script that the user triggers can run every minute as well and can increment the number of hits using javascript and the last known rate by guessing.

Here is the only problem I see: what if the rate of hits goes way down and someone refreshes the page and sees that the number of hits has actually gone down since their refresh! Am I missing something?

One solution (if this really is a problem) would be to have the counter show a value well behind the actual number of hits and have it count up. When it reaches the last known number of hits, it talks to the database to find the new number of hits, and again starts counting up until it reaches the new number of hits. Then, it just repeats the process of asking the database, and counting up.

And yes, I am using one row.

Re: Continuously updated hit counter

Posted: Wed Oct 27, 2010 9:15 pm
by coledenkensohn
Quick tangent:

Is there a way to "push" data? For example, when I get an email and have gmail open, it automatically comes in and shows the new email. Is this simple enough to accomplish or is google just a crazy giant that does what it wants? If it's at all possible, how!

Re: Continuously updated hit counter

Posted: Wed Oct 27, 2010 11:07 pm
by Jonah Bron
I guess I didn't quite explain enough. There is no extra row. The Javascript in the page calculates the rate. Every time it fetches the count (every ten seconds), it calculates the rate of increase against the count it had before fetching. Like I said before, if it was 10, then it updates 10 seconds later and it's 20, it knows that the count is increasing 1 count/second. So, between fetching, it increments the count by one every second.

Re: Continuously updated hit counter

Posted: Thu Oct 28, 2010 2:52 am
by coledenkensohn
Got it. I am just worried that by doing that the count could go down when it refreshes if it somehow overestimates the rate. I'll have to work though it, thank you!

Re: Continuously updated hit counter

Posted: Thu Oct 28, 2010 5:09 am
by josh
This 'rate' thing sounds like unnecessary complexity. I'd focus on getting a working hit counter, before even worrying about any of this ajax. You have a lot of requirement, like tracking IPs. Is the ajax part really the priority here?

Once you have it working, does it really need to update more often than 10 seconds? Gmail works by periodically polling for new emails every 15-30 seconds.

Re: Continuously updated hit counter

Posted: Thu Oct 28, 2010 11:16 am
by Jonah Bron
josh wrote:Once you have it working, does it really need to update more often than 10 seconds? Gmail works by periodically polling for new emails every 15-30 seconds.
I do agree. I was using ten seconds to simplify the example (10/10 = 1/1).