Jump to content

Making a simple counter?


bpops

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]
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.