Jump to content

Archived

This topic is now archived and is closed to further replies.

bpops

Making a simple counter?

Recommended Posts

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.
[code]<?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
?>[/code]

Share this post


Link to post
Share on other sites
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:

[url=http://www.phpfreaks.com/forums/index.php/topic,102741.0.html]http://www.phpfreaks.com/forums/index.php/topic,102741.0.html[/url]

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

thanks!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.