Jump to content


Photo

Making a simple counter?


  • Please log in to reply
26 replies to this topic

#1 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 03:55 AM

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!

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 28 July 2006 - 04:06 AM

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.



Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 04:12 AM

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 :)

#4 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 04:31 AM

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..
"Syntax error" .. WHERE THE **** IS IT?!

#5 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 04:36 AM

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!

#6 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 04:38 AM

so its not 1 page where it reads off the databse to display the info its also 500 pages?
"Syntax error" .. WHERE THE **** IS IT?!

#7 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 04:42 AM

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
"Syntax error" .. WHERE THE **** IS IT?!

#8 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 04:45 AM

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...orials/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 :))


#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 28 July 2006 - 04:54 AM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 04:57 AM

o ya good point red arrow i forgot about that....
"Syntax error" .. WHERE THE **** IS IT?!

#11 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 04:59 AM

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?

#12 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 05:01 AM

database and login..... most likly..
"Syntax error" .. WHERE THE **** IS IT?!

#13 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 05:05 AM

But I'm sure the counters increment for non-members too.
Maybe not, I dunno.

Thanks for all the help legohead :)

#14 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 05:09 AM

no problem im kinda curious to how this will work too.. as i have a simular idea for my site....
"Syntax error" .. WHERE THE **** IS IT?!

#15 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 28 July 2006 - 05:11 AM

ok i teach you both ok 1 min ok
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#16 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 05:14 AM

cool! ....
"Syntax error" .. WHERE THE **** IS IT?!

#17 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 05:14 AM

ok i teach you both ok 1 min ok


awesome, I'm anxious to see :)

#18 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 28 July 2006 - 05:51 AM

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.


database.php

<?php

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

?>


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

so example index.php

<?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>";

}

?>

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
<?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']");
}

?>


view.php
<?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']";

}

?>












Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#19 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 28 July 2006 - 05:59 AM

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!

#20 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 28 July 2006 - 06:00 AM

ok mate good luck
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users