1

Is it possible to use JQuery & PHP to create a "like" button that a user could click and it would add +1 to a "number of likes" database (or even text file) and disable the "like" button for that user so that the user could only click it once? I was browsing around and found some information about writing cookies with JQuery:

http://jquery-howto.blogspot.com/2010/09/jquery-cookies-getsetdelete-plugin.html

Perhaps, when a like button is clicked, it could write a cookie to the user's computer that would prevent them from future clicks? It just basically needs to be that the user could click the like button, it adds a count to some type of database, and it disables the button for the user. Pretty simple I would imagine - there may already be some type of plugin for this, but I haven't found any. Any ideas?

Thanks!

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
opes
  • 1,778
  • 1
  • 16
  • 22
  • 2
    Remember never to trust your users, or JavaScript (client-side)-based security. – David Thomas Nov 20 '10 at 00:23
  • Right. Then maybe a more purely PHP solution with JQuery effects would be a better option. Maybe limit the clicks based off of IP? – opes Nov 20 '10 at 00:27

2 Answers2

2

jquery:

$("button").click(function(){
  $(this).remove();
  $.post('count.php');
});

though the user can just reload the page, so any real validation needs to happen on the php side.

generalhenry
  • 17,227
  • 4
  • 48
  • 63
  • That makes sense. And the validation should limit it by IP address then? Or is there another way? – opes Nov 20 '10 at 00:35
  • @Dan: The best way is if your users are registered on the site.. Otherwise, a combination of Cookies and an IP history would do well. – drudge Nov 20 '10 at 00:47
  • Do not restrict by IP address alone since it is incredibly common for many users to be on one IP. How crappy would it be if one person on a network voted and everyone else was blocked ;) Instead restrict by session, other cookie or username. – Justin Johnson Nov 20 '10 at 00:50
  • Thanks for the input. I will keep this in mind when I build the validation for it. I'd hate to build a complete user registration system for this one page, but sessions/cookies could be a good alternative option. Thanks for your help, everyone! – opes Nov 20 '10 at 00:57
  • 1
    @Dan, you might want to take a read of this page: [how to uniquely identify computers visiting my website?](http://stackoverflow.com/questions/216542/how-do-i-uniquely-identify-computers-visiting-my-web-site) and [Flash cookies](http://www.google.co.uk/search?q=flash+cookies+site:stackoverflow.com). – David Thomas Nov 20 '10 at 01:21
  • just have count.php store username/likeditem pair along with the tally. ajax will handle the immediate removal of like button, checking the db when rendering the page will do it in the future – jon_darkstar Nov 20 '10 at 01:35
0

You may want to look at jQuery's one() function. It allows you to bind an event for only one invocation. Here's an example I'd run on page load.

if (likedBefore) {
  $("button").addClass("liked");
}
else {
  $("button").one("click", function() {
     $(this).addClass("liked");
     $.post("count.php");
  });
}

Validating server side is a bit more difficult. It really depends on how secure you need this to be.

JonMR
  • 586
  • 3
  • 18