View Full Version : PHP - Page Counter

08-10-2008, 07:38 AM

I am trying to create a page counter in PHP.

Table structure:

Table Name: tbl_counter
Column Name:total_views
Column Datatype: integer

I am using the following SQL query on the page.

UPDATE tbl_counter SET total_views=total_views+1

I know its weird but, sometimes the counter increases twice instead of 1, any idea why?

08-10-2008, 07:53 AM
The SQL is correct, so the only way that can happen if there is multiple loads of the page. This could be caused by inclusions depending on you're page setups of course. Chances are though, it was just another visitor or a spider crawling the net.
If you're on you're local system, I'd check you're log files. Apache really shouldn't accept connections from the outside world in you're testing environment. I left mine open once. It was bad.

08-10-2008, 07:55 AM
I am using this locally, and no one has incoming access to my system.

I know its weird, but I could not find a fool-proof solution to this.

08-10-2008, 08:04 AM
Its definitely from multiple executions of the file though. Either its an external influence, or its being included multiple times into another file. There is not enough information to determine if its a problem being created through the program code.

08-10-2008, 08:09 AM
whats the solution then?

08-10-2008, 08:29 AM
There isn't really a solution. Debugging you're code is the best I can offer, if you have a decent IDE with a debugger (I use the zend eclipse IDE, its not great, but I've used much worse), you can step through each step of you're code to see how many times its executing. I'll warn you though, you can't use external libraries when you're debugging, so you'll need to comment out you're mysql connections and queries. But at least you'll see it as you step over it which can help to identify what the problem is and how you can solve it.

08-10-2008, 11:31 AM
@cancer10, have a try by emptying your table and making a fresh "Page view"