Jump to content


Photo

Making a simple counter?


  • Please log in to reply
26 replies to this topic

#21 lukelambert

lukelambert
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 July 2006 - 06:05 AM

Or if you just want to use a cookie...

<?php
$cc_name = "counter"; // Name of the cookie
$cc_expires = time() + 3600; // Expires in an hour
$cc_item = "[" . $_GET['id'] . "]"; // Item identifier in [XX] format

if ($_COOKIE[$cc_name])
{
	$cc_value = $_COOKIE[$cc_name];
	
	if (strpos($cc_value, $cc_item) === false) // If the user has not visited this page
	{
		// Increase view count in database or whatever here
		// Something like: mysql_query("UPDATE item_table SET (hits = hits + 1) WHERE (id = " . $_GET['id'] . ")", $connection);
		$cc_value .= $cc_item; // Add the item to the cookie
	}
}
else
	$cc_value = $cc_item; // Add the item to the cookie

setcookie($cc_name, $cc_value, $cc_expires); // Create or re-create the cookie
?>

Basically everytime the user loads the page, this code scans the cookie to see if they have been here or not according to the item id. Also, the cookie refreshes everytime the user visits a new item so the expiration time is always updating. You would need to have a hits column in your item_table (or whatever it's named). In addition, $connection should be your actual database connection.

I don't know if there is a length limit on cookies but this one might get a little long if the user is visiting a lot of pages.

#22 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 31 July 2006 - 02:48 AM

Sorry this is a bit of a late reply, but Thanks lukelambert. I think this is exactly the sort of thing I was looking for. I'm going to implement this now. The cookie might get large, sure, but atleast it's ONE cookie :)

#23 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 31 July 2006 - 04:47 AM

lukelambert,

I've been trying for quite a while now to get this code working, however I'm having an issue.

I noticed inside your ELSE statement that there was now mysql query to add a count. So if the user goes the site for the first time, the first item (before the cookie is created) won't be incremented.
So the solution SEEMED easy. I just added a mysql query (the same one) into that else statement.
But for someone when I do that, it runs that query when it isn't supposed to run. So now when someone visits a page, it will increment 2 or even 3 counts. I'm not sure why.. is there a specific reason that this might be?



#24 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 31 July 2006 - 03:45 PM

I posted this late last night and it's since gotten off the main page, I just wanted to see if any of you daytimers understood the problem stated in my last post.

thanks :)

#25 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 31 July 2006 - 07:44 PM

I hate to do this, but I gotta give this a bump.

If anyone can figure out my problem (two posts up), I'd be so appreciative. I spent 3 hours last night trying to figure this out and it's been driving me nuts! :P

#26 lukelambert

lukelambert
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 02 August 2006 - 05:54 PM

Sorry bpops, I haven't checked back here in a while. I completely overlooked the fact that it would not increase the post count the first time they visit (which will probably be the only time on individual items so that code was completely useless). I think I have fixed it now though.
<?php
$cc_name = "counter"; // Name of the cookie
$cc_expires = time() + 3600; // Expires in an hour
$cc_item = "[" . $_GET['id'] . "]"; // Item identifier in [XX] format
$cc_inc = false; // Whether the number of hits for that item should be incremented.

if ($_COOKIE[$cc_name])
{
	$cc_value = $_COOKIE[$cc_name];
	
	if (strpos($cc_value, $cc_item) === false) // If the user has not visited this page
	{
		$cc_value .= $cc_item; // Add the item to the cookie
		$cc_inc = true;
	}
}
else
{
	$cc_value = $cc_item; // Add the item to the cookie
	$cc_inc = true;
}

if ($cc_inc)
{
	// Increase view count in database or whatever here
	// Something like: mysql_query("UPDATE item_table SET (hits = hits + 1) WHERE (id = " . $_GET['id'] . ")", $connection);
}

setcookie($cc_name, $cc_value, $cc_expires); // Create or re-create the cookie
?>


#27 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 02 August 2006 - 06:00 PM

thanks luke :)

I had modified your code a bit, and now I'm having a new problem.. if  you're interested in taking a look, i Have another post here:

http://www.phpfreaks...c,102741.0.html

But I will try your new code as well and see if for some reason that fixes this strange problem I'm having.

thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users