Forum Moderators: open

Message Too Old, No Replies

Click counter

         

dannad11

6:14 pm on Aug 11, 2010 (gmt 0)

10+ Year Member



Is there a Javascript counter which will enable users to click a button if they like the page and displays how many times it has been pressed?
If it's possible only unique IPs, don't want people continually pressed it to increase the number.

Thanks.

MichaelBluejay

3:33 am on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not with pure JavaScript, no. JS can't write files or access databases, so there's nowhere to store the data.

The typical way to do what you want is with server-side scripting like PHP or Perl:

<form action="incrementCounter.cgi" method=post>
<input type=submit value="I like this!">
</form>


Then the Perl file:
#! /usr/bin/perl

open(FILE,'<counter.txt');
undef $/;
$count=<FILE>;
close(FILE);

$count++;

open(FILE,'>count.txt');
print FILE $count;
close(FILE);


Above is untested, but should work. This doesn't weed out clicks from the same IP, that's a bit more work.

JevgeniBogatyrjov

11:49 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



And it is also impossible to pull the client's IP address with JS.
You could create a cookie with JS when the counter is pressed, but it would not guarantee getting multiple votes from one user.
So you must find a way to get that done server side