bpops Posted August 2, 2006 Share Posted August 2, 2006 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: [url=http://www.b-pops.com/wow]http://www.b-pops.com/wow[/url]. 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):[code]<?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?>[/code]I am testing this new code at the following location:[url=http://www.b-pops.com/wow/beta/most.php]http://www.b-pops.com/wow/beta/most.php[/url]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! Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/ Share on other sites More sharing options...
ober Posted August 2, 2006 Share Posted August 2, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-67956 Share on other sites More sharing options...
king arthur Posted August 2, 2006 Share Posted August 2, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-67957 Share on other sites More sharing options...
bpops Posted August 2, 2006 Author Share Posted August 2, 2006 [quote author=ober link=topic=102741.msg408231#msg408231 date=1154539416]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.[/quote]Thanks for the reply :)I have tried this, and it results in the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-67958 Share on other sites More sharing options...
bpops Posted August 2, 2006 Author Share Posted August 2, 2006 [quote author=king arthur link=topic=102741.msg408232#msg408232 date=1154539458]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?[/quote]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. Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-67962 Share on other sites More sharing options...
bpops Posted August 2, 2006 Author Share Posted August 2, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-67982 Share on other sites More sharing options...
ober Posted August 2, 2006 Share Posted August 2, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-68006 Share on other sites More sharing options...
bpops Posted August 2, 2006 Author Share Posted August 2, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/16347-updating-a-hit-counter-mysql-help/#findComment-68023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.