Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to prevent a user clicking 'like' multiple times

I'm implementing a like feature for a site I'm working on. A user doesn't have to be logged in and they can either like or dislike a particular page. At the end of it all I'd like to be able to populate a list of articles or stories with the most likes.

I have a very simple method that currently uses an onclick javascript function to update a database via ajax and a php function. This is working ok. What I'd like to do is prevent a user from spamming the button.
At first I thought of maybe getting the IP address, storing that in the database and then running a check. Is there a better way?

like image 577
null Avatar asked Dec 09 '22 12:12

null


2 Answers

Technically there isn't a bomb proof way to do so. You could get pretty close by allowing one vote per ip-useragent combination. You'd have to implement this on the server side.

PHP Example

 $concienceKey = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['USER_AGENT']);

 $query = "SELECT COUNT(*) FROM clickConcience WHERE key = `" . $concienceKey . "`";

 //run your query
 //.....and get the $count;
 //

 //already voted!
 if($count > 0){
      echo 'already voted';
      return false;
 }

 //remember entry
 $insert = "INSERT INTO clickConcience (key, datetime) VALUES (`" . $concienceKey . "`, NOW())";

 //run your query
 //.....and insert
 //

 return true;
like image 50
Bas Kuis Avatar answered Dec 11 '22 01:12

Bas Kuis


straight forward answer, you won't be able to do it.

If I really want to SPAM your "like" button, I will find a way to do so, especially if you're not forcing me to be signed in (I used to write pretty good bots and were pretty efficient spamming big link submission sites).

Javascript will only stop mediocre spammers or sock puppet account holders. As a spammer I can circumvent your Javascript pretty easily, either by programming a time-based robot to like your post, or by sending requests directly to your server (I will not even load your site).

What you need to do, if you really want to prevent spammers from spamming this feature efficiently (efficiency is the keyword here because spammers can still spam your feature, but their likes won't count) is to log every IP that likes a post along with its geographical information (it's not always 100% accurate, but it's a good start) and then run a process in the background that checks for suspicious origins and penalize such likes (either by assigning them less value, or just subtracting them from the total count).

For example if your main audience is people living in the United States, but one post gets a bunch of likes from Mexico, Salvador, India, Australia, Russia, then it's more than likely that there's a spammer behind a proxy or a network similar to TOR and he/she can change his/her IP address at his/her will.

After a few hundred thousand records, you'll have a good base to start blacklisting IP addresses. I usually use R programming language to get statistical information about my databases.

But then again, a good spammer could use a list of IP addresses of compromised computers coming from your audience's country or geographical location, and use those IPs to abuse the feature. Those bots are harder to spot, but you can analyze previous posts and come up with useful metrics as "Likes/comment ratio".

If one post has a huge number of likes, but low number of comments, then it's very probable that someone spammed it, but then again I can program my bot to like AND post a comment so the numbers look natural.

I'm not sure what kind of project you're working on, but if it's something similar to link submission, do not rank (whatever your users are liking) by the number of likes.

The number of likes should only be a factor, you can take a look at how HackerNews or Reddit rank the posts (those projects are open source), but it's a combination between multiple factors.

like image 33
ILikeTacos Avatar answered Dec 11 '22 01:12

ILikeTacos