watisit
watisit

Reputation: 303

Ensuring users do not spam "like" button

My friends and I are creating a petition board and i'm adding a like/dislike function to it. I intend to make it such that only users can like/dislike it. Problem is, I do not know how to ensure that the users do not spam the button multiple times and how to register which user has liked/disliked which topic. Below is my code so far.

EDIT: Thanks I am creating the likes/dislikes table right now. But now I have to compare the users with the database to see if they have previously liked a comment. I know I have to use WHERE (to check both likes and dislikes table) but i am not sure how to combine it with IF.

<?php
include connect.php

if (isset($_POST['like']) || isset($_POST['dislike'])) 
{
    if($_SESSION['signed_in']){
        if (isset($_POST['like'])) {
            $sql="UPDATE 
                    topics
                SET
                    likes=likes+1,
                WHERE
                    id=topic_id";

            echo "You liked it";
        }

        elseif (isset($_POST['dislike'])) {
            $sql="UPDATE 
                    topics
                SET
                    dislikes=dislikes+1,
                WHERE
                    id=topic_id";

            echo "You disliked it";
        }
    }
    else{
        echo 'Please log in.'
}

?>

Upvotes: 3

Views: 707

Answers (2)

solarised
solarised

Reputation: 114

The following solutions are possible, which can be used together:

  • If you use a registration/login mechanism, then you could internally setup some counting mechanism so each user can like once per petition (like Bgi suggested).
  • You could store a cookie, preventing him for further liking, even if he creates a new user.
  • Of course people can delete cookies or use other browsers, so you could hash their IP with e.g. md5 and compare the hash if that hash was already using the petition.
  • Of course multiple people can share the same IP, so IP hashing might not always be a good solution. Alternatively, you could use facebook API, and require that people have some amount of friends or something in order to verify their authenticy.

You will never fully be able to get rid of spammers, depends on how specific is your petition. Hence the more you want to prevent exploiting from using the petition, the less anonymous it will be.

Upvotes: 0

Bgi
Bgi

Reputation: 2494

You should have a table of "likes" with the following columns.

"article_id", "user_id", the primary key should contain both columns

Every time a user likes an article, INSERT INTO likes VALUES($article_id, $user_id); It will fail if someones Likes twice, thanks to the primary key.

Every time a user dislikes, DELETE FROM likes WHERE article_id = $article_id AND user_id = $user_id. That will allow the user to like again if he wants.

To get the number of Likes for an article, run a SELECT COUNT(*) as nb_of_likes FROM likes WHERE article_id = $article_id instead of storing the number in the article table.

Makes sense ?

Upvotes: 5

Related Questions