Jump to content


Photo

Updating a hit Counter - mySQL - Help!


  • Please log in to reply
7 replies to this topic

#1 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 02 August 2006 - 05:11 PM

Ok, I've had a couple of threads on this, but once they get over a few posts, people tend to not help, so this is a final effort to try and clear up my problem. I'll explain everything in one post here, and hopefully someone can  help me out.

I run a little World of Warcraft site that let's people make their own warcraft items (just for displaying on a page). It's just something for fun. The site is here: http://www.b-pops.com/wow. It's all done in php and mysql.

Now, I want each item to have its own counter. But I don't want someone to be able to hit reload and increment the counter multiple times. So I am using cookies. I have the following code on the item page (which displays an item based on ?id=XX in the url):


<?php
//Code cut out here where I connect to the database, select the item according to idea, and extract($result).

	//Counter Cookie
	//Set cookie info
	$cc_name = "wow_counter"; // Name of the cookie
	$cc_expires = time() + 3600; // Expires in an hour
	$cc_item = "[$id]"; // Item identifier in [XX] format
	$cc_value = $_COOKIE[$cc_name];	
	if (strstr($cc_value, $cc_item) === FALSE) // If the user has not visited this page
	{
		// Increase view count in database  
		$hits = $hits + 1;
		$test = "updated! id: $id, #hits: $hits";  //just a diagnostic that will display in the html 
           //so i can see if an item has been updated, and with what value.
		mysql_query("UPDATE wow_items SET hits=$hits WHERE id=$id");
		$cc_value .= $cc_item; // Add the item to the cookie
	}
  	setcookie($cc_name, $cc_value, $cc_expires,'/wow/','.b-pops.com',0); // Create or re-create the cookie

?>


I am testing this new code at the following location:
http://www.b-pops.co...w/beta/most.php
The number of views you see here are of course just ones I've done in testing. On the items page, I've also implemented a diagnostic so you can SEE the cookie that we're using (you'll see what I mean after viewing a couple of items)

Ok, so if a user goes to an item page (and has no cookie), then the counter is incremented as expected, and the cookie is created. When you go to another item, that counter is incremented. Refresh and it stays the same. However, occasionally (and that's the worst part is that this is occasional), the counter will increment 2 or 3 hits! I cannot for the life of me figure out why.

If anyone has ANY clue why this is happening, OR if anyone knows a better or other way of doing this whole process, I would be eternally grateful. I've been busting my brain over this for days now and am becoming quite frustrated.

Thanks so much in advance!

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 02 August 2006 - 05:23 PM

Well, you could remove one piece of the puzzle by modifying your SQL:

mysql_query("UPDATE wow_items SET hits=hits+1 WHERE id=$id");

That way you do your math based off of what is in the database and not by what junk might be sitting in the $hits variable.

Info: PHP Manual


#3 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 02 August 2006 - 05:24 PM

Do you have any traffic at all to your site? Can you check server logs to see if something else has accessed those pages and thereby increased the hit counters?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#4 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 02 August 2006 - 05:25 PM

Well, you could remove one piece of the puzzle by modifying your SQL:

mysql_query("UPDATE wow_items SET hits=hits+1 WHERE id=$id");

That way you do your math based off of what is in the database and not by what junk might be sitting in the $hits variable.


Thanks for the reply :)
I have tried this, and it results in the same problem.

#5 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 02 August 2006 - 05:28 PM

Do you have any traffic at all to your site? Can you check server logs to see if something else has accessed those pages and thereby increased the hit counters?


I do have plenty of traffic, but it is all to the /wow/ directory. This beta directory I pointed you to is private.. at least in th extent that this is the only place that I've ever posted it.

I don't  have site statistic as detailed as to tell me the pages that have been seen, but I'm sure that is not the problem. I've been having this problem since I began doing this counter, when the beta directory wasn't posted even here.

Thanks for your reply.

#6 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 02 August 2006 - 05:45 PM

ober,
just so you can see the problem persists, I've changed the code to hits=hits+1.

If you guys can't see the problem, would you know of another way of doing this? Or is this the best way of doing a counter?

Thanks again :)

#7 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 02 August 2006 - 06:18 PM

I don't personally see the bug. 

Is this a bug you're noticing WHILE testing?  Could it be that a web crawler finds your site randomly and is increasing the counter?

Info: PHP Manual


#8 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 02 August 2006 - 06:40 PM

Yes, it is something I notice while testing.

I doubt its a crawler, since I can change the directory name, try it again and the problem persists. And the count only goes up on items I am testing.

But the fact that some of you php gurus don't see the bug at least makes me feel better than I'm not just missing something.

Perhaps I should just ignore this problem and make the counter live anyway.

Thanks for all the help guys. If there are any last suggestions or revelations, feel free to say em :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users