Jump to content

Archived

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

bpops

Making a simple counter?

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!

Share this post


Link to post
Share on other sites
if a user press a link do you want the database to get a number to add up while they press that link.

post your url or pm it to me so i can see the page what your doing ok.


Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites
does the user log in? cuz then you could use timestamps and sessions to check...... i dont know code but thats pointing in the right direction..

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
so its not 1 page where it reads off the databse to display the info its also 500 pages?

Share this post


Link to post
Share on other sites
you could create a new table and use ip address....

table
row0 = id (as almost every table does)
row1 = ip (user ip address)
row2 = page (page they visted)
row3 = date (date and time)


then call to that to find out if theve been there that day

Share this post


Link to post
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 :))

Share this post


Link to post
Share on other sites
Ip adddress will only work for a short time becouse most isp supliers suply dynamic ip address that change a lot.

the best bet is to inplement a login system ok.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
But I'm sure the counters increment for non-members too.
Maybe not, I dunno.

Thanks for all the help legohead :)

Share this post


Link to post
Share on other sites
no problem im kinda curious to how this will work too.. as i have a simular idea for my site....

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=102123.msg404946#msg404946 date=1154063513]
ok i teach you both ok 1 min ok
[/quote]

awesome, I'm anxious to see :)

Share this post


Link to post
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]











Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
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?

Share this post


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

Share this post


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

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.