Jump to content

Making a simple counter?


bpops

Recommended Posts

Hi all,
I'm interested in making a simple counter for my site, however the catch is that I want each page to have a separate counter (kept in a mysql db), and I don't want users to be able to reload to increase the count..

So I figured the best way to do this is with cookies, but not sure how I should go about it. The pages I have are for different entries in a db, and they're all id'ed (with AUTO_INCREMENT), so each has a unique number. So i could make individual cookies, but it seems like that's going to far..

any suggestions?

Thanks in advance!
Link to comment
Share on other sites

I'd prefer not to post a link since it much a user-run site, and the site is slightly less friendly than this one :P  .. I'm doing this for your sake. hehe

But let me try to explain more clearly since I did not do so before.
I have a site with a database with many items in the database. Say there are 500 items. Each item is ID'ed 1-500.
I want each item (which already is display on each own page via www.------.com/item.php?id=XX) to have its own hit counter. The hit (int unsigned) is already a column in my table and ready to go.
When a user visits the page of an item, I want the counter to increment. But if the user reloads or comes back in an hour, I DONT want the counter to increment. But if the user goes to a different items' page, I do want it to increment that counter.

I know a way of doing this, but it seems messy. Basically I could have a separate cookie for EACH page with an expiration date of say a day.. But if the user looks at 100 items, they will get 100 cookies. Is this not a terribly messy (and possibly controversial) way of doing things?

I was just wondering if there was a better way to make sure people aren't refreshing and incrementing the counters where they shouldn't be.

Thanks for the help :)
Link to comment
Share on other sites

No, the user does not log in.

I'm sure a session or cookie is the right way to go with this, and if I just had one page this would be no problem. The problem lies in the fact that I have many pages with individual counters...
Thanks!
Link to comment
Share on other sites

You're right, I could do that. And that might be the best solution in the end.
I'd prefer to not deal with MySQL for checking this if possible though.
I also found a good example of what I'm doing. I was reading some counter tutorials right here on phpfreaks:
http://www.phpfreaks.com/tutorials/27/1.php

Look at the top of the page it says how many views that page has gotten. Click reload on your browser. Count doesn't change. THAT's what I want :))
Link to comment
Share on other sites

The ip address would be ok actually since I don't care if the views are incremented more than once by the same person after a day or so, I just don't want people clicking the refresh button over and over to increase the counter.

But anyone knows how the guys here at phpfreaks do it?
Link to comment
Share on other sites

There might be small bugs as i done this live but i think your get the idear ok.

what this scripts do is let you see the links on a page then the link will goto the update page then update the database with the users id then redirect that user to the page that they wanted to see but at the same time the hits will show on all the pages that hits was echoed and the users can not refresh to increase the hits.

[code]

database.php

<?php

$db=mysql_connect("localhost","xxxxxx","xxxxxx);
mysql_select_db("xxxxx",$db);

?>

[/code]

Show link to the correct page and the counter for links.

so example index.php

[code]
<?php

include("database.php");

$query="SELECT * form hits where member_id='$member_id'";
$result=mysql_query($query);

while($record=mysql_fetch_assoc($result)){

$hits=$record['hits'];

echo"<table border="4">
<td><a href='update.php?member_id=$member_id&l=link">link 1 </a>$hits<td>
<td><a href='update.php?member_id=$member_id&l=link">link 2 </a>$hits<td>
<td><a href='update.php?member_id=$member_id&l=link">link 3 </a>$hits<td>
<td><a href='update.php?member_id=$member_id&l=link">link 4 </a>$hits<td>
<td><a href='update.php?member_id=$member_id&l=link">link 5 </a>$hits<td></table>";

}

?>
[/code]

update the database but then redirect them to the page they wanted so the user can not just refresh and increase more conter results.

update.php
[code]
<?php

include("database.php");

if($_GET['l']=="link"){

$update="update hits set hits=hits+1 where member_id='".$_GET['member_id']."'";
$result=mysql_query($update);

header("location: view.php?member_id=$_GET['member_id']");
}

?>
[/code]


view.php
[code]
<?php

include("database.php");


$query="select what_ever where member_id='".$_GET['member_id']."'";
$result=mysql_query($query);

while($member_info=mysql_fetch_assoc($result) {


echo " <br> $member_info['name'] <br>";

echo " <br> $member_info['age'] <br> ";

echo " <br> $member_info['dob'] <br> ";

}

$select_hits="select * from hits where member_id='".$_get['member_id']."'";
$hits_result=mysql_query($select_hits);

while($get_hits_count=mysql_fetch_assoc($hits_result) {

echo " <br> current page hits is $get_hits_count['hits']";

}

?>
[/code]











Link to comment
Share on other sites

Thanks redarrow,

I'm not sure if this is going to work for me as most/all of my hits come form outside my page (people link directly to the individual item pages). And if I understand correctly what you're doing, it makes things a bit tricky.
It's getting late and I'm not thinking clearly though, hehe, so I'm going to go over this code again tomorrow.

Thanks!
Link to comment
Share on other sites

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

[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

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

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

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

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?

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.